本博客中介绍了两种整合方式,分别是xml配置和注解
mysql
mysql-connector-java
runtime
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
com.alibaba
druid
1.0.19
这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖。
MyBatis-Spring-Boot-Starter依赖将会提供如下
就是说,使用了该Starter之后,只需要定义一个DataSource即可(application.properties中可配置),它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。
数据源配置
注意要加?characterEncoding=UTF-8,否则部分数据库会中文乱码
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = XXXX
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
package com.gwd;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import com.alibaba.druid.pool.DruidDataSource;
@SpringBootApplication
public class SpringBootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTestApplication.class, args);
}
@Autowired
private Environment env;
//destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
@Bean(destroyMethod = "close")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setInitialSize(2);//初始化时建立物理连接的个数
dataSource.setMaxActive(20);//最大连接池数量
dataSource.setMinIdle(0);//最小连接池数量
dataSource.setMaxWait(60000);//获取连接时最大等待时间,单位毫秒。
dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效的sql
dataSource.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效
dataSource.setTestWhileIdle(true);//建议配置为true,不影响性能,并且保证安全性。
dataSource.setPoolPreparedStatements(false);//是否缓存preparedStatement,也就是PSCache
return dataSource;
}
}
bean
package com.gwd.domain;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
Service
package com.gwd.service;
import com.gwd.domain.User;
public interface StuService {
public User getById(int id);
}
ServiceImpl(和Spring中无二,务必加@Service)
package com.gwd.service.impl;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gwd.dao.StuMapper;
import com.gwd.domain.User;
import com.gwd.service.StuService;
@Service
public class StuServiceImpl implements StuService,Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private StuMapper stuMapper;
@Override
public User getById(int id) {
// TODO Auto-generated method stub
User user = stuMapper.selectById(id);
return user;
}
}
Controller
package com.gwd.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.Controller;
import com.gwd.domain.User;
import com.gwd.service.StuService;
@Controller
public class StuController {
@Autowired
private StuService stuService;
@RequestMapping("/index")
@ResponseBody
public String index() {
User stu = stuService.getById(1);
return stu.getName();
}
}
#加上?characterEncoding=UTF-8,否则部分数据库中文会乱码
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = XXXX
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
#mapper.xml在Resource下的话配置如下,否则配置为:mybatis.mapper-locations=classpath:com/gwd/movie/dao/mapper/*.xml
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.gwd.domain
logging.file=/log/springBootTest.log
server.port=8090
package com.gwd.dao;
import org.apache.ibatis.annotations.Mapper;
import com.gwd.domain.User;
@Mapper
public interface StuMapper {
User selectById(int id);
}
注:这边的@Mapper注解也可以不用,直接在运行类上加上@MapperScan(basePackages= {"xxx.xxx.mapper"}),并且推荐使用后者,比较方便,具体如下图
该文件放置到resources文件夹下面,并且需要在Application.properties文件中配置文件地址
id, name
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = 19940315
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.gwd.domain
logging.file=/log/springBootTest.log
server.port=8090
package com.gwd.dao;
import org.apache.ibatis.annotations.*;
import com.gwd.domain.User;
@Mapper
public interface StuMapper {
@Select("select * from stu where id = #{id}")
@Results(id = "userMap", value = {
@Result(column = "id", property = "id", javaType = Integer.class),
@Result(property = "name", column = "name", javaType = String.class)
})
User selectById(@Param("id")int id);
}