Springboot-整合mybatis

·  阅读 47

pom.xml

      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
复制代码

application.yaml

spring:
  datasource:
    username: root
    password: 12345678
    url: jdbc:mysql://localhost:3306/javaweb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    # 合并多个DruidDataSource的监控数据
    useGlobalDataSourceStat: true
    # web-stat-filter:
    # enabled: false
mybatis:
  type-aliases-package: com.faa.lpfproject01.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml
复制代码

pojo

package com.faa.lpfproject01.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private int id ;
    public String name ;


}
复制代码

UserMapper

package com.faa.lpfproject01.mapper;

import com.faa.lpfproject01.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {

    List<User> getUserlist();

    User queryUserbById(int id);

    int addUser(User user);

    int addUser2(User user);

    int updateUser(User user);

    int deleteUser(int id);


}
复制代码

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.faa.lpfproject01.mapper.UserMapper">


    <select id="getUserlist" resultType="User">
        select * from t_user;
    </select>

    <select id="queryUserbById" resultType="User" parameterType="int">
        select * from t_user where id = #{id};
    </select>


    <insert id="addUser" parameterType="User">
        insert into t_user(id,name) values(#{id},#{name});
    </insert>

    <insert id="addUser2" parameterType="User">
        insert into t_user(name) values(#{name});
    </insert>


    <update id="updateUser" parameterType="User">

        update t_user set name = #{name} where id = #{id};


    </update>

    <delete id="deleteUser" parameterType="int">

        delete from t_user where id = #{id};

    </delete>

</mapper>
复制代码
package com.faa.lpfproject01.controller;

import com.faa.lpfproject01.mapper.UserMapper;
import com.faa.lpfproject01.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class HelloController {
    @Autowired
    UserMapper userMapper ;


    @RequestMapping("/getlist2")
    public List<User> getList2(){
        List<User> list = userMapper.getUserlist();
        return list ;
    }

    @RequestMapping("/getListById2/{id}")
    public User getListById(@PathVariable("id") int id){
        User user = userMapper.queryUserbById(id);
        return  user ;
    }

    @RequestMapping("/addUser2")
    public String addUser2(){
        User user = new User();
        user.setId(16);
        user.setName("帕可适当");
        userMapper.addUser(user);
        return "ok";
    }


    @RequestMapping("/addUser3")
    public String addUser3(){
        User user = new User();
        user.setName("快开始第");
        userMapper.addUser(user);
        return "ok";
    }
    @RequestMapping("/updateUser2")
    public String updateUser2(){
    //报错 缺少id
        User user = new User();
        user.setId(10);
        user.setName("dsadd");
        userMapper.updateUser(user);
        return "ok";
    }
    @Transactional
    @RequestMapping("/updateUser3")
    public String updateUser3(){

        User user = new User();
        user.setId(8);
        user.setName("无双大蛇多");
        userMapper.updateUser(user);
        int idsd = 1/0 ;
        User user2 = new User();
        user2.setId(13);
        user2.setName("奥术大师多");
        userMapper.updateUser(user2);
        return "ok";
    }

    @RequestMapping("/deleteUser2")
    public String deleteUser(){
        userMapper.deleteUser(11);
        return "ok";
    }

}
复制代码
分类:
Android
标签:
收藏成功!
已添加到「」, 点击更改