SpringBoot整合Mybatis,及其添加事物
1.一些说明
1.1根据目前查到的信息整合mybatis有两种方式,一种是纯注解的方式,另一种就是需要配置xml的方式。本篇文章只涉及到后者,如果需要前者的请自行搜索。
1.2 事物是使用的添加注解的方式,根据查到的资料,注解方式的事物也适用其他orm。
1.3 springboot的版本是2.X,数据库使用的mysql,开发工具使用的intellj(学生免费,每年需要激活)
2.创建项目
新建springboot项目–Spring Initializr,默认下一步,勾选web,mysql,mybatis(就不需要修改pom.xl),完成。
4 配置application.properties(没有使用xxx.yml)
//导入mybatis配置文件
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
//导入mapper配置文件
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = xxx
spring.datasource.password = xxx
5.mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
6.mapper.xml配置
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.example1.demo.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.example1.demo.bean.User" >
select * from user where id = #{id}
</select>
</mapper>
7
需要在启动类加入@MapperScan(“com.example1.demo.mapper”)
用来扫描的mapper借口
user类
package com.example1.demo.bean;
public class User {
public User(){
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
userMapper接口
package com.example1.demo.mapper;
import com.example1.demo.bean.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User getUserById(Integer id);
}
controller类
package com.example1.demo.test;
import com.example1.demo.bean.User;
import com.example1.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("Test")
class test1 {
@Autowired
public UserMapper userMa;
@RequestMapping("getUser")
public String ttt(){
User u = userMa.getUserById(1);
return u.toString();
}
}
本文详细介绍了如何在SpringBoot 2.x项目中整合Mybatis,并通过注解方式配置事务。首先,文章提到整合Mybatis有两种方式,本文侧重于使用XML配置的方式。接着,介绍了从创建SpringBoot项目、配置application.properties、设置mybatis-config.xml和mapper.xml的步骤。最后,提醒在启动类中使用@MapperScan注解来扫描mapper接口,并提到了user类、userMapper接口和controller类的相关内容。
1508

被折叠的 条评论
为什么被折叠?



