SpringBoot集成mybatis配置
正文前先来一波福利推荐:
福利一:
百万年薪架构师视频,该视频可以学到很多东西,是本人花钱买的 VIP 课程,学习消化了一年,为了支持一下女朋友公众号也方便大家学习,共享给大家。
福利二:
毕业答辩以及工作上各种答辩,平时积累了不少精品 PPT,现在共享给大家,大大小小加起来有几千套,总有适合你的一款,很多是网上是下载不到。
获取方式:
微信关注 精品 3 分钟 ,id 为 jingpin3mins,关注后回复 百万年薪架构师 ,精品收藏 PPT 获取云盘链接,谢谢大家支持!
----------------------- 正文开始 ---------------------------
一个有趣的现象:传统企业大都喜欢使用 hibernate, 互联网行业通常使用 mybatis;之所以出现这个问题感觉与对应的业务有关,比方说,互联网的业务更加的复杂,更加需要进行灵活性的处理,所以 mybatis 的灵活性特点更为适合其
作为技术选型的优势;
mybatis 初期使用比较麻烦,需要各种配置文件、实体类、dao 层映射关联、还有一大推其它配置。当然 mybatis 也发现了这种弊端,初期开发了可以根据表结果自动生产实体类、配置文件和 dao 层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理 dao 层和配置文件等,发展到最顶端就是今天要讲的这种模式了。
mybatis-spring-boot-starter 就是 springboot+mybatis 可以完全注解不用配置文件,也可以简单配置轻松上手。
mybatis-spring-boot-starter
任何模式都需要首先引入 mybatis-spring-boot-starter 的 pom 文件。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
一、极简 xml 版本 【个人推荐使用这种方式,松耦合性】
极简 xml 版本保持映射文件的老传统,优化主要体现在不需要实现 dao 的实现层【只需要定义接口类和方法】,系统会自动根据方法名在映射文件中找对应的 sql【由 namespace 和名称坐标确定】。
1、配置
pom.xml 文件【部分】:
<dependencies>
<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>
<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>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
application.properties
新增以下配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
指定了 mybatis 基础配置文件和实体类映射文件的地址
mybatis-config.xml 配置
<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>
这里也可以添加一些 mybatis 基础的配置
2、添加 User 的映射文件
<mapper namespace="com.neo.mapper.UserMapper" > <resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="userName" property="userName" jdbcType="VARCHAR" /> <result column="passWord" property="passWord" jdbcType="VARCHAR" /> <result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/> <result column="nick_name" property="nickName" jdbcType="VARCHAR" /> </resultMap><sql id="Base_Column_List" ><span style="color: rgba(0, 0, 0, 1)"> id, userName, passWord, user_sex, nick_name </span></sql> <select id="getAll" resultMap="BaseResultMap" ><span style="color: rgba(0, 0, 0, 1)"> SELECT </span><include refid="Base_Column_List" /><span style="color: rgba(0, 0, 0, 1)"> FROM users </span></select> <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" ><span style="color: rgba(0, 0, 0, 1)"> SELECT </span><include refid="Base_Column_List" /><span style="color: rgba(0, 0, 0, 1)"> FROM users WHERE id </span>=<span style="color: rgba(0, 0, 0, 1)"> #{id} </span></select> <insert id="insert" parameterType="com.neo.entity.UserEntity" ><span style="color: rgba(0, 0, 0, 1)"> INSERT INTO users (userName,passWord,user_sex) VALUES (#{userName}, #{passWord}, #{userSex}) </span></insert> <update id="update" parameterType="com.neo.entity.UserEntity" ><span style="color: rgba(0, 0, 0, 1)"> UPDATE users SET </span><<span style="color: rgba(0, 0, 255, 1)">if</span> test="userName != null">userName = #{userName},</<span style="color: rgba(0, 0, 255, 1)">if</span>> <<span style="color: rgba(0, 0, 255, 1)">if</span> test="passWord != null">passWord = #{passWord},</<span style="color: rgba(0, 0, 255, 1)">if</span>><span style="color: rgba(0, 0, 0, 1)"> nick_name </span>=<span style="color: rgba(0, 0, 0, 1)"> #{nickName} WHERE id </span>=<span style="color: rgba(0, 0, 0, 1)"> #{id} </span></update> <delete id="delete" parameterType="java.lang.Long" ><span style="color: rgba(0, 0, 0, 1)"> DELETE FROM users WHERE id </span>=<span style="color: rgba(0, 0, 0, 1)">#{id} </span></delete>
</mapper>
其实就是把上个版本中 mapper 的 sql 搬到了这里的 xml 中了
3、编写 Dao 层的代码【定义接口】
public interface UserMapper { List<UserEntity> getAll(); UserEntity getOne(Long id); void insert(UserEntity user); void update(UserEntity user); void delete(Long id); }
对比上一步这里全部只剩了接口方法
二、无配置文件注解版【耦合性高】
就是一切使用注解搞定。
1 添加相关 maven 文件【同上】
2、application.properties
添加相关配置
mybatis.type-aliases-package=com.neo.entity spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = root
springboot 会自动加载 spring.datasource.* 相关配置,数据源就会自动注入到 sqlSessionFactory 中,sqlSessionFactory 会自动注入到 Mapper 中,对了你一切都不用管了,直接拿起来使用就行了。
在启动类中添加对 mapper 包扫描@MapperScan
@SpringBootApplication @MapperScan("com.neo.mapper") public class Application {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { SpringApplication.run(Application.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args); }
}
或者直接在 Mapper 类上面添加注解@Mapper
, 建议使用上面那种,不然每个 mapper 加个注解也挺麻烦的
3、开发 Mapper
第三步是最关键的一块,sql 生产都在这里
public interface UserMapper {@Select(</span>"SELECT * FROM users"<span style="color: rgba(0, 0, 0, 1)">) @Results({ @Result(property </span>= "userSex", column = "user_sex", javaType = UserSexEnum.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">), @Result(property </span>= "nickName", column = "nick_name"<span style="color: rgba(0, 0, 0, 1)">) }) List</span><UserEntity><span style="color: rgba(0, 0, 0, 1)"> getAll(); @Select(</span>"SELECT * FROM users WHERE id = #{id}"<span style="color: rgba(0, 0, 0, 1)">) @Results({ @Result(property </span>= "userSex", column = "user_sex", javaType = UserSexEnum.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">), @Result(property </span>= "nickName", column = "nick_name"<span style="color: rgba(0, 0, 0, 1)">) }) UserEntity getOne(Long id);</span><span style="color: rgba(0, 0, 0, 1)">
}
为了更接近生产我特地将 user_sex、nick_name 两个属性在数据库加了下划线和实体类属性名不一致,另外 user_sex 使用了枚
4、使用
上面三步就基本完成了相关 dao 层开发,使用的时候当作普通的类注入进入就可以了
@RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest {@Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> UserMapper UserMapper; @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testInsert() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { UserMapper.insert(</span><span style="color: rgba(0, 0, 255, 1)">new</span> UserEntity("aa", "a123456"<span style="color: rgba(0, 0, 0, 1)">, UserSexEnum.MAN)); UserMapper.insert(</span><span style="color: rgba(0, 0, 255, 1)">new</span> UserEntity("bb", "b123456"<span style="color: rgba(0, 0, 0, 1)">, UserSexEnum.WOMAN)); UserMapper.insert(</span><span style="color: rgba(0, 0, 255, 1)">new</span> UserEntity("cc", "b123456"<span style="color: rgba(0, 0, 0, 1)">, UserSexEnum.WOMAN)); Assert.assertEquals(</span>3<span style="color: rgba(0, 0, 0, 1)">, UserMapper.getAll().size()); }<br></span><span style="color: rgba(0, 0, 0, 1)">}</span></pre>