Spring Boot简单xml配置集成mybatis

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot简单xml配置集成mybatis相关的知识,希望对你有一定的参考价值。

一、xml配置版
1.properties文件中增加的配置:
  mybatis.config-locations=classpath:mybatis/mybatis-config.xml
  mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
2.mybatis-config.xml文件的配置
  mapper文件里面的jdbcType重命名
<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>
3.添加User的映射文件
<?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.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" > id, userName, passWord, user_sex, nick_name </sql> <select id="getAll" resultMap="BaseResultMap" > SELECT <include refid="Base_Column_List" /> FROM users </select> <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" > SELECT <include refid="Base_Column_List" /> FROM users WHERE id = #{id} </select> <insert id="insert" parameterType="com.neo.entity.UserEntity" > INSERT INTO users (userName,passWord,user_sex) VALUES (#{userName}, #{passWord}, #{userSex}) </insert> <update id="update" parameterType="com.neo.entity.UserEntity" > UPDATE users SET
  <if test="userName != null">userName = #{userName},</if>
  <if test="passWord != null">passWord = #{passWord},</if>
  nick_name = #{nickName} WHERE id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Long" >
  DELETE FROM users WHERE id =#{id}
</delete>
</mapper>

 

4.dao层的接口
public interface UserMapper { 
    List<UserEntity> getAll(); 
    UserEntity getOne(Long id); 
    void insert(UserEntity user); 
    void update(UserEntity user); 
    void delete(Long id); 
}

 

5.单元测试:见注解方式集成mybatis事例
 
6.可能会出现的问题:
技术分享

解决方法;

技术分享

 










以上是关于Spring Boot简单xml配置集成mybatis的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot(XML配置)和Jasypt集成

Spring Boot 集成 logback日志

Spring Boot集成ActiveMQ

mybatic与spring结合的事务管理

Mybatis集成到spring boot

Spring Boot