0%

MyBatis及 SpringBoot解决代码冗余

SpringBoot整合JDBCTemplate以及事务管理

– 贴一篇博客https://blog.csdn.net/wx5040257/article/details/79465307

MyBatis

1
2
3
4
5
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>

⭐⭐ 配置注意一个问题:mybatis-config和applicaton.properties中都要配置mysql数据库的连接信息

1
2
3
4
5
##数据库连接信息(基于JDBC)
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/parking_lot_swpu?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = Qwer;t592665
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5
6
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/parking_lot_swpu?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="Qwer;t592665"/>
</dataSource>

引入Mapper – 为了解决代码冗余问题

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class UserDao {
private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getInstance();

public User getUserById(Integer id) {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = (User) sqlSession.selectOne("org.javaboy.mybatis.mapper.UserDao.getUserById", id);
sqlSession.close();
return user;
}

public Integer addUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int insert = sqlSession.insert("org.javaboy.mybatis.mapper.UserDao.addUser", user);
sqlSession.commit();
sqlSession.close();
return insert;
}

public Integer addUser2(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int insert = sqlSession.insert("org.javaboy.mybatis.mapper.UserDao.addUser2", user);
sqlSession.commit();
sqlSession.close();
return insert;
}

public Integer deleteUserById(Integer id) {
SqlSession ssqlSession = sqlSessionFactory.openSession();
int desete = sqlSession.delete("com.swpu.htht.mapper.UserDao.deleteUserById",id);
sqlSession.commit();
sqlSession.close();
return delete;
}

public Integer updateUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int delete = sqlSession.delete("org.javaboy.mybatis.mapper.UserDao.updateUser", user);
sqlSession.commit();
sqlSession.close();
return delete;
}

public List<User> getAllUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> users = sqlSession.selectList("org.javaboy.mybatis.mapper.UserDao.getAllUser");
sqlSession.close();
return users;
}
}

然后,和这个UserDao对应的,还有一个UserDaoMapper.xml:

当然,观察上面的UserDao.class,你可以发现大部分代码冗余,其实这些都可以模块化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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="org.javaboy.mybatis.mapper.UserDao">

<select id="getUserById" resultType="org.javaboy.mybatis.model.User">
select * from user where id=#{id};
</select>

<insert id="addUser" parameterType="org.javaboy.mybatis.model.User">
insert into user (username,address) values (#{username},#{address});
</insert>

<insert id="addUser2" parameterType="org.javaboy.mybatis.model.User">
<selectKey resultType="java.lang.String" keyProperty="id" order="BEFORE">
select uuid();
</selectKey>
insert into user (id,username,address) values (#{id},#{username},#{address});
</insert>

<delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>

<update id="updateUser" parameterType="org.javaboy.mybatis.model.User">
update user set username = #{username} where id=#{id};
</update>

<select id="getAllUser" resultType="org.javaboy.mybatis.model.User">
select * from user;
</select>
</mapper>

我们说过UserDao.class的问题,现在我们将之模块化:原理很简单,就是Spring所说的动态代理。我们可以将当前方法简化成一个接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.javaboy.mapper;

public interface UserMapper {
User getUserById(Integer id);

Integer addUser(User user);

Integer addUser2(User user);

Integer deleteUserById(Integer id);

Integer updateUser(User user);

List<User> getAllUser();
}

这个接口对应的Mapper文件如下(注意,UserMapper.xml 和 UserMapper 需要放在同一个包下面,但是分别在project的java和Resource中)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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="org.javaboy.mybatis.mapper.UserMapper">

<select id="getUserById" resultType="org.javaboy.mybatis.model.User">
select * from user where id=#{id};
</select>
<insert id="addUser" parameterType="org.javaboy.mybatis.model.User">
insert into user (username,address) values (#{username},#{address});
</insert>
<insert id="addUser2" parameterType="org.javaboy.mybatis.model.User">
<selectKey resultType="java.lang.String" keyProperty="id" order="BEFORE">
select uuid();
</selectKey>
insert into user (id,username,address) values (#{id},#{username},#{address});
</insert>

<delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>

<update id="updateUser" parameterType="org.javaboy.mybatis.model.User">
update user set username = #{username} where id=#{id};
</update>

<select id="getAllUser" resultType="org.javaboy.mybatis.model.User">
select * from user;
</select>
</mapper>

使用上述方式,就可以代替UserDao了。

然后我们需要在MyBatis全局配置中配置UserMapper:

1
2
3
<mappers>
<package name="org.javaboy.mybatis.mapper"/>
</mappers>

最后就可以加载配置文件,获取UserMapper,并调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
public class Main {
/**
*提供入口
*/
public static void main(String[] args) {
SqlSessionFactory instance = SqlSessionFactoryUtils.getInstance();
SqlSession sqlSession = instance.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> allUser = mapper.getAllUser();
System.out.println(allUser);
}
}

resultMap – 自定义映射的结果集

先看一个基本用法:

1
2
3
4
5
<resultMap id="MyResultMap" type="org.javaboy.mybatis.model.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="address" property="address"/>
</resultMap>

在上面的ResultMap中,id用来描述主键,column是数据库查的列名,property则是对象中的属性名。

然后在查询结果中,定义返回值使用这个ResultMap:

1
2
3
4
5
<select id="getUserById" resultMap="MyResultMap">
select * from user

where id=#{id};
</select>

也可以在resultMap中指定要调用的构造方法,指定方式如下:

1
2
3
4
5
6
<resultMap id="MyResultMap" type="org.javaboy.mybatis.model.User">
<constructor>
<idArg column="id" name="id"/>
<arg column="username" name="username"/>
</constructor>
</resultMap>

上面的resultMap表示使用两个参数的构造方法去构造一个实例。⭐注意:name属性表示构造方法中的变量名,默认情况下,变量名是arg0, arg1,或者parm0, parm1, 如果需要自定义,我们可以在构造方法中,手动加上@Param注解。

1
2
3
4
public User(@Param("id") Integer id, @Param("username") String username) {
this.id = id;
this.username = username;
}