springboot使用mybatis
本文目录
- springboot使用mybatis
- 引入maven依赖
- 新增数据库配置
- 创建数据库表
- 实体类
- 编写mapper接口层
- 通过xml文件
- mapper中直接使用注解
- 编写mapper.xml
- IDEA创建mybatis的xml文件
- parameterType
- 启动类
- controller调用
引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringBoot web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis 支持 SpringBoot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
新增数据库配置
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demomybatis?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
spring.datasource.username=xx
spring.datasource.password=xx
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
mybatis.mapper-locations = classpath:mapping/*Mapper.xml
创建数据库表
Person数据库如下图
Address数据库如下图
实体类
@Data
public class Person {
private String id;
private String name;
}
@Data
public class Address {
private String id;
private String addrName;
}
编写mapper接口层
通过xml文件
@Mapper //表示这是Mybatis的mapper类
@Repository
public interface PersonMapper {
List<Person> queryUserList();
Person queryUserByID(String id);
int addUser(Person person);
int updateUser(Person person);
int deleteUser(String id);
}
mapper中直接使用注解
@Mapper //表示这是Mybatis的mapper类
@Repository
public interface AddressMapper {
@Select("SELECT * FROM address WHERE addrName = #{name}")
Address findByName(@Param("name") String name);
@Insert("INSERT INTO address VALUES (#{id}, #{addrName},)")
int insert(@Param("id") String id, @Param("addrName") String addrName);
}
编写mapper.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.example.demo.PersonMapper">
<select id="queryUserList" resultType="com.example.demo.Person">
select * from person
</select>
<select id="queryUserByID" resultType="com.example.demo.Person">
select * from person where id= #{id}
</select>
<insert id="addUser" parameterType="com.example.demo.Person">
insert into person(id,name) values (#{id},#{name})
</insert>
<update id="updateUser" parameterType="com.example.demo.Person">
update person set name=#{name} where id=#{id}
</update>
<delete id="deleteUser" parameterType="java.lang.String">
delete from person where id=#{id}
</delete>
</mapper>
IDEA创建mybatis的xml文件
File---->Settings
Editor---->File And Code Templates —>中间选中Files ----->点击+号,添加模板 ----->输入模板名字:Name:XML File.xml (name可以自定义) ------>后缀名extension: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="">
</mapper>
选中文件夹----->New-------->找到XML File,就可以创建xml文件了
parameterType
Mybatis的Mapper文件中的select、insert、update、delete元素中有一个parameterType属性,用于对应的mapper接口方法接受的参数类型
启动类
在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan
@SpringBootApplication
@MapperScan("com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
controller调用
@CrossOrigin
@RestController
@RequestMapping("/define")
public class DemoController {
@Autowired
PersonMapper personMapper;
@GetMapping("/getmybatis")
public List<Person> getmybatis() {
List<Person> res= personMapper.queryUserList();
return res;
}
@GetMapping("/queryUserList")
public List<Person> queryUserList() {
List<Person> users = personMapper.queryUserList();
return users;
}
@GetMapping("/queryUserByID")
public Person queryUserByID() {
Person user = personMapper.queryUserByID("2");
return user;
}
@GetMapping("/addUser")
public String addUser() {
Person p = new Person();
p.setId("4");
p.setName("zzz");
personMapper.addUser(p);
return "增加用户完毕";
}
@GetMapping("/updateUser")
public String updateUser() {
Person p = new Person();
p.setId("2");
p.setName("zzz");
personMapper.updateUser(p);
return "修改用户完毕";
}
@GetMapping("/deleteUser")
public String deleteUser() {
personMapper.deleteUser("4");
return "删除用户完毕";
}
@Autowired
AddressMapper addressMapper;
@RequestMapping("/addAddress")
public int addUser(String userName, String age) {
return addressMapper.insert(age,userName);
}
@RequestMapping("/getAddress")
public Address getUserByName(String userName) {
return addressMapper.findByName(userName);
}
}