整合环境:idea+gradle4.8(maven)+springboot2.0+mysql5
更多关于SpringBoot的总结请点击:SpringBoot使用总结
如果使用gradle作为管理工具,则在build.gradle中配置以下代码,引入mybatis的依赖:
//Mybatis依赖
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
//jdbc依赖
runtime('mysql:mysql-connector-java')
如果使用maven作为管理工具,则在pom.xml中配置以下代码,引入mybatis的依赖:
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
在导入完mybatis的依赖后,我们需要在主配置文件中添加关于mybatis和数据库的相关配置:
注意,jdbc版本在8.0及其之上时,如果不配置时间区间serverTimezone=GMT%2B8
的话,在使用mybatis时将会报一下错误:
Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
# mapper.xml文件完整路径扫描
mybatis.mapper-locations=classpath:com/shanyutech/bucaogui/mapper/*.xml
# 数据库连接名
spring.datasource.username=root
# 数据库连接密码
spring.datasource.password=123
# 数据库连接驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库名称以及编码表和时间区间
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
我建立的包结构如图:
当配置依赖完毕后,需要在运行主类上加@MapperScan注解去告诉配置Mapper包的位置,以便于找到接口和xml配置文件:
@SpringBootApplication
@MapperScan("com.example.springboot_mybatis_test.mapper")
public class SpringbootMybatisTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisTestApplication.class, args);
}
}
这一步十分地关键,如果不进行资源文件的配置,会造成mapper.xml文件没有被加载到classes中。
如果是使用gradle进行项目管理,可以在build.gradle中添加以下代码将xml文件读取
//xml资源文件读取
sourceSets {
main {
resources {
//可以将java目录下的所有非.java资源打包到classes下
srcDirs = ['src/main/java', 'src/main/resources']
}
}
}
如果是使用maven作为项目管理工具,则在pom.xml中添加以下代码:
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
resources>
build>
现在所有的配置均已完毕,我们使用熟悉的SpringMVC以及三层架构完成一次对数据库的请求,测试其中是否有问题出现:
以下为项目结构:
/**
* @Auther: ARong
* @Date: 2018/12/20 17:49
* @Description: 查询用户
*/
@RestController
public class userController {
//注入UserService
@Autowired
private UserService userService;
/**
* @auther: Arong
* @description: 请求service层获取UserListVTO,以json格式返回给前台
* @param
* @return: com.example.springboot_mybatis_test.vto.UserListVTO
* @date: 2018/12/20 21:08
*/
@GetMapping(value="/getUserList")
public UserListVTO getUserList(){
UserListVTO userListVTO = userService.getUserList();
return userListVTO;
}
}
/**
* @Auther: ARong
* @Date: 2018/12/20 17:52
* @Description: 获取UserListVTO的service接口
*/
public interface UserService {
UserListVTO getUserList();
}
/**
* @Auther: ARong
* @Date: 2018/12/20 17:54
* @Description: UserService的实现类,完成业务逻辑
*/
@Service
public class UserServiceImpl implements UserService {
//注入UserMapper
@Autowired
private UserMapper userMapper;
/**
* @auther: Arong
* @description:请求UserMapper,获得User集合并封装成UserListVTO对象返回给Controller
* @param
* @return: com.example.springboot_mybatis_test.vto.UserListVTO
* @date: 2018/12/20 21:10
*/
@Override
public UserListVTO getUserList(){
List<User> userList = userMapper.getUserList();
UserListVTO userListVTO = new UserListVTO(userList);
return userListVTO;
}
}
/**
* @Auther: ARong
* @Date: 2018/12/20 17:56
* @Description: User的代理接口
*/
@Repository
@Mapper
public interface UserMapper {
List<User> getUserList();
}
<mapper namespace="com.example.springboot_mybatis_test.mapper.UserMapper">
<select id="getUserList" resultType="com.example.springboot_mybatis_test.entity.User">
select * from user;
select>
mapper>
/**
* @Auther: ARong
* @Date: 2018/12/20 17:44
* @Description: User实体类
*/
public class User implements Serializable{
//id
private int id;
//name
private String name;
//school
private String school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
/**
* @Auther: ARong
* @Date: 2018/12/20 21:02
* @Description: 包装UserList,返回给前台
*/
public class UserListVTO {
//真实的UserList对象
private List<User> userList;
//在创建时确定userList的实例
public UserListVTO(List<User> userList){
this.userList = userList;
}
//getter & setter
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
运行启动类,访问http://localhost:8080/getUserList,出现以下Json数据,mybatis整合成功: