SpringBoot实验笔记
实验一、SpringBoot整合mybatis
(一)、SpringBoot的数据访问(mysql和mybatis依赖)
①sprngboot项目的pom.xml文件中引入的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>integrate-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>integrate-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
②数据库的创建(mybatisdb)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s1V9tSNT-1633015286097)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210930222336627.png)]
③application.properties中配置数据库连接配置
# mysql数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
④创建数据库表对应的实体类
Role类(用于配置方式)
public class Role {
private Integer ID;
private String ROLE_NAME;
private String ROLE_DESC;
}
User类(用于注解方式)
public class User {
private Integer id;
private String username;
private String birthday;
private String sex;
private String address;
}
(二)、注解方式整合mybatis
①创建Mapper接口文件UserMapper
package com.example.integratemybatis.mapper;
import com.example.integratemybatis.domain.User;
import org.apache.ibatis.annotations.*;
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
public User findById(Integer id);
@Insert("insert into User(username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})")
public int insertUser(User user);
@Update("update user set username=#{username} where id=#{id}")
public int updateUser(User user);
@Delete("delete from user where id=#{id}")
public int deleteUser(Integer id);
}
注意:@Mapper,当编写的Mapper接口过多时,需要重复为每一个接口文件添加@Mapper注解,为了避免麻烦,可以直接在SpringBoot项目的启动类上添加@MapperScan(“接口所在包名的全限定名称”)。
②测试类(已通过)
package com.example.integratemybatis;
import com.example.integratemybatis.domain.Role;
import com.example.integratemybatis.domain.User;
import com.example.integratemybatis.mapper.RoleMapper;
import com.example.integratemybatis.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class IntegrateMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void testDelete() {
int num2=userMapper.deleteUser(43);
if(num2>0){
System.out.println("删除数据成功");
}else{
System.out.println("删除数据失败");
}
}
@Test
void testFind(){
User user=userMapper.findById(42);
System.out.println("姓名:"+user.getUsername());
System.out.println("出生日期:"+user.getBirthday());
System.out.println("性别:"+user.getSex());
System.out.println("地址:"+user.getAddress());
}
@Test
void testInsert(){
User user1=new User();
user1.setUsername("乔峰");
user1.setBirthday("1008-02-27 17:47:08");
user1.setSex("男");
user1.setAddress("蒙古");
int num=userMapper.insertUser(user1);
if(num>0){
System.out.println("插入数据成功!");
}else{
System.out.println("插入数据失败!");
}
}
@Test
void testUpdate(){
User user=userMapper.findById(46);
user.setUsername("张无忌");
int num1=userMapper.updateUser(user);
if(num1>0){
System.out.println("修改数据成功");
}else{
System.out.println("修改数据失败");
}
}
}
(三)、配置文件方式整合mybatis
①创建Mapper接口文件RoleMapper
package com.example.integratemybatis.mapper;
import com.example.integratemybatis.domain.Role;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface RoleMapper {
public List<Role> selectRole(Integer ID);
public int updateRole(Role role);
}
②创建Mybatis关于Mapper操纵的xml文件user-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.integratemybatis.mapper.RoleMapper">
<select id="selectRole" resultMap="roleMap">
select * from role
</select>
<resultMap id="roleMap" type="com.example.integratemybatis.domain.Role">
<id column="ID" property="ID"/>
<result column="ROLE_NAME" property="ROLE_NAME"/>
<result column="ROLE_DESC" property="ROLE_DESC"/>
</resultMap>
</mapper>
③在核心配置文件中配置关于mybatis的xml映射文件
说明:我们在项目中编写的xml配置文件SpringBoot并不会自动扫描,必须在全局配置文件中添加其映射文件的路径的配置,同时,如果在自定义的mybatis的xml配置文件中没有使用实体类的全限定名称则需要添加实体类别名映射路径
# 配置mybatis的xml的配置文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
# 配置xml映射文件中指定的实体类别名路径(mapper.xml文件中使用的是全路径实体类名称则不需要配置此项)
# mybatis.type-aliases-package=com.example.interatemybatis.domain
④测试类(已通过)
package com.example.integratemybatis;
import com.example.integratemybatis.domain.Role;
import com.example.integratemybatis.domain.User;
import com.example.integratemybatis.mapper.RoleMapper;
import com.example.integratemybatis.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class IntegrateMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private RoleMapper roleMapper;
@Test
void selectRole(){
List<Role> roleList=roleMapper.selectRole(1);
System.out.println("名字:"+roleList.get(0).getROLE_NAME());
System.out.println("角色描述:"+roleList.get(0).getROLE_DESC());
}
}
(四)两种SpringBoot整合Mybatis的方式对比
使用用注解的方式比较适合简单的增删改查操作;而使用配置文件的方式稍显麻烦,但是对于复杂的数据操作却显得比较实用。两种方式可以混合使用
(五)项目目录结构图
|