Mybatis(四):實戰
阿新 • • 發佈:2018-10-31
準備工作
pom.xml
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${springboot-mybatis.version}</version> </dependency> <!--mysql連線--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
mybatis-config.xml
<?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="Map" type="java.util.Map"/> <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: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml type-aliases-package: love.ning.yangxuyue.domain
Mybatis使用
XML版
返回自增ID
// 方式一 // 元素設定 useGeneratedKeys 和 keyProperty 屬性 // useGeneratedKeys="true" 表示給主鍵設定自增長 // 當資料寫入到資料表時,其自增主鍵自動地填充到實體引數的 id 屬性<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO User(username, password) VALUES (#{username}, #{password}); </insert> // 方式二 <insert id="insert" parameterType="User" > <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="productId"> // MySQL語法 SELECT LAST_INSERT_ID() </selectKey> INSERT INTO User(username, password) VALUES (#{username}, #{password}); </insert>
批量處理
collection:指定要遍歷的集合:
list型別的引數會特殊處理封裝在map中,map的key就叫list
item:將當前遍歷出的元素賦值給指定的變數
separator:每個元素之間的分隔符
open:遍歷出所有結果拼接一個開始的字元
close:遍歷出所有結果拼接一個結束的字元
index:索引。
1、遍歷list的時候是index就是索引,item就是當前值
2、遍歷map的時候index表示的就是map的key,item就是map的值
{變數名}就能取出變數的值也就是當前遍歷出的元素
插入
<insert id="insert" parameterType="List" > INSERT INTO tableName(id, name) VALUES <foreach collection="list" item="item" index="index" separator=","> (#{item.id},#{item.name}) </foreach> </insert>
查詢
<select id="getByIds" parameterType="List" resultType="User"> SELECT * FROM tableName WHERE id IN <foreach collection="list" item="id" separator="," open="(" close=")"> #{id} </foreach> </select>
更新
<update id="update" parameterType="List"> <foreach collection="list" item="item" separator=";" index="index"> UPDATE tableName SET name = #{item.name} email = #{item.email} WHERE xxx <foreach> </update>
註解版
增刪改查
// 查詢 @Select("SELECT * FROM user WHERE name = #{name}") User findByName(@Param("name") String name); // 返回結果的繫結 @Select("SELECT * FROM users WHERE id = #{id}") @Results({ @Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), @Result(property = "nickName", column = "nick_name") }) UserEntity getOne(Long id); @Select("SELECT * FROM users") @Results({ @Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class), @Result(property = "nickName", column = "nick_name") }) List<UserEntity> getAll(); // 新增 @Insert("INSERT INTO USER(NAME, AGE) VALUES(# {name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})") int insertByMap(Map<String, Object> map); @Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})") int insertByUser(User user); @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); // 修改 @Update("UPDATE user SET age=#{age} WHERE name = #{name}") void update(User user); // 刪除 @Delete("DELETE FROM user WHERE id = #{id}") void delete(Long id);