springboot-整合druid+mybatis

项目环境配置

添加依赖

  <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.5version>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.1version>
        dependency>

properties文件配置

# mybatis
mybatis.type-aliases-package=com.link.seckill.domain
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapperLocations = classpath:com/link/seckill/dao/*.xml
# druid
spring.datasource.url=jdbc:mysql://localhost:3306/seckill?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.filters=stat
spring.datasource.maxActive=2
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20

实例测试是否配置成功

  • 创建如图所示的数据表
    springboot-整合druid+mybatis_第1张图片
  • 创建实体类
public class User {
    private int id;
    private String name;
    //setter/getter...
  • dao层通过注解来配置
@Mapper
public interface UserDao {

    @Select("select * from sk_test where id = #{id}")
    public User getById(@Param("id") int id);

    @Insert("insert into sk_test(id, name)values(#{id}, #{name})")
    public int insert(User user);

}

注:
@Param的作用:mybatis就提供了@Param这个注解来完成命名,传入参数的时候,
mybatis才能通过参数名字将数据传入#{}中
@Param注解的用法解析

  • service层

本层设置数据的读取及事务的检测,id为1的记录已经存在数据库中,在插入id=2的数据后在插入id=1的,将产生主键冲突,事务回滚id=2不会被保存

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User getById(int id) {
        return userDao.getById(id);
    }

    @Transactional
    public boolean tx() {
        User u1= new User();
        u1.setId(2);
        u1.setName("sb");
        userDao.insert(u1);

        User u2= new User();
        u2.setId(1);
        u2.setName("sd");
        userDao.insert(u2);
        return true;
    }
}
  • controller层
@Controller
public class SampleController {
    @Autowired
    UserService userService;

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet() {
        User user = userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx() {
        userService.tx();
        return Result.success(true);
    }
}

你可能感兴趣的:(springboot)