1. 程式人生 > >xml型單資料來源Mybatis.md

xml型單資料來源Mybatis.md


  1. 例項需求:使用者資訊(使用者名稱、別名、密碼、性別、郵箱、建立時間),實現簡單增刪改查

  2. 技術要點:

  1. mybatis類型別名設定
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<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>

  1. springboot整合mybatis配置
mybatis.config-locations:mybatis配置檔案放置路徑
mybatis.mapper-locations:mybatis對應mapper指令碼對映路徑
mybatis.type-aliases-package:mybatis對應實體包路徑


mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.lm.entity

  1. springboot單資料來源配置
spring.datasource.driverClassName:資料來源驅動類
spring.datasource.url:資料來源url
spring.datasource.username:使用者名稱
spring.datasource.password:密碼


spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.32.128:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root

  1. springboot與mybatis整合配置
在可執行檔案下掃描mapper包

/**
 * 應用啟動
 * 
 * @author liangming.deng
 * @date 2017年6月20日
 *
 */

@SpringBootApplication
@MapperScan("com.lm.mapper")
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
  1. 進行controller層的開發:注入相應的Mapper介面進行操作

附上Mapper.xml和Mapper介面程式碼:


/**
 * mybatis中mapper介面
 * @author liangming.deng
 * @date   2017年6月21日
 *
 */
@Mapper
public interface UserMapper {
	List<UserEntity> getAll();
 
	UserEntity getUserById(Long id);
 
	void insert(UserEntity user);
 
	void update(UserEntity user);
 
	void delete(Long id);
}

<?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.lm.mapper.test.UserMapper">
	<resultMap id="BaseResultMap" type="com.lm.entity.UserEntity">
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="userName" property="userName" jdbcType="VARCHAR" />
		<result column="nickName" property="nickName" jdbcType="VARCHAR" />
		<result column="passWord" property="passWord" jdbcType="VARCHAR" />
		<result column="email" property="email" jdbcType="VARCHAR" />
		<result column="regTime" property="regTime" jdbcType="DATE" />
		<result column="sex" property="sexEnums" javaType="com.lm.enums.SexEnums" />
	</resultMap>
 
	<sql id="Base_Column_List">
		id, userName, nickName,passWord, email,regTime,sex
	</sql>
 
	<select id="getAll" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List" />
		FROM user
	</select>
 
	<select id="getUserById" parameterType="java.lang.Long" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List" />
		FROM user
		WHERE id = #{id}
	</select>
 
	<insert id="insert" parameterType="com.lm.entity.UserEntity">
		INSERT INTO
		user
		(userName, nickName,passWord, email,regTime,sex)
		VALUES
		(#{userName},#{nickName}, #{passWord},#{email}, #{regTime},#{sexEnums})
	</insert>
 
	<update id="update" parameterType="com.lm.entity.UserEntity">
		UPDATE
		user
		SET
		<if test="userName != null">userName = #{userName},</if>
		<if test="passWord != null">passWord = #{passWord}</if>
		WHERE
		id = #{id}
	</update>
 
	<delete id="delete" parameterType="java.lang.Long">
		DELETE FROM
		user
		WHERE
		id =#{id}
	</delete>
 
</mapper>