mybatis15 mapper方式 代码
生活随笔
收集整理的這篇文章主要介紹了
mybatis15 mapper方式 代码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
UserMapper.java
package cn.itcast.mybatis.mapper;import java.util.List;import cn.itcast.mybatis.po.User; import cn.itcast.mybatis.po.UserQueryVo;public interface UserMapper {//根據(jù)用戶id查詢用戶信息public User findUserById(int id) throws Exception;//根據(jù)用戶名稱 查詢用戶信息public List<User> findUserByName(String username) throws Exception;//自定義查詢條件查詢用戶信息public List<User> findUserList(UserQueryVo userQueryVo) throws Exception;//查詢用戶,使用resultMap進行映射public List<User> findUserListResultMap(UserQueryVo userQueryVo)throws Exception;//查詢用戶,返回記錄個數(shù)public int findUserCount(UserQueryVo userQueryVo) throws Exception;//插入用戶public void insertUser(User user)throws Exception;//刪除用戶public void deleteUser(int id) throws Exception;//修改用戶public void updateUser(User user) throws Exception;}UserMapper.xml
<?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代理開發(fā)時將namespace指定為mapper接口的全限定名--> <mapper namespace="cn.itcast.mybatis.mapper.UserMapper"> <!-- 在mapper.xml文件中配置很多的sql語句,執(zhí)行每個sql語句時,封裝為MappedStatement對象 mapper.xml以statement為單位管理sql語句--><!-- 將用戶查詢條件定義為sql片段建議對單表的查詢條件單獨抽取sql片段,提高公用性注意:不要將where標簽放在sql片段(公用性差)--><sql id="query_user_where"><!-- 如果 userQueryVo中傳入查詢條件,再進行sql拼接--><!-- test中userCustom.username表示從userQueryVo讀取屬性值--><if test="userCustom!=null"><if test="userCustom.username!=null and userCustom.username!=''">and username like '%${userCustom.username}%'</if><if test="userCustom.sex!=null and userCustom.sex!=''">and sex = #{userCustom.sex}</if><!-- 根據(jù)id集合查詢用戶信息 --><!-- 最終拼接的效果:SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND id IN (開始 16,22,25循環(huán) )結(jié)束collection:集合的屬性,userQueryVo的ids屬性open:開始循環(huán)拼接的串close:結(jié)束循環(huán)拼接的串item:每次循環(huán)取到的對象separator:每兩次循環(huán)中間拼接的串--><foreach collection="ids" open=" AND id IN ( " close=")" item="id" separator=",">#{id} <!-- 16,循環(huán) --></foreach><!-- 如果拼接 SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND (開始 id = 16 OR id = 22 OR id = 25循環(huán) )結(jié)束 <foreach collection="ids" open=" AND ( " close=")" item="id" separator="OR">id = #{id} <!-- id = 16 ,循環(huán) --></foreach>--><!-- 還有很的查詢條件 --></if></sql><!-- 定義resultMap,列名和屬性名映射配置id:mapper.xml中的唯一標識 type:最終要映射的pojo類型--><resultMap id="userListResultMap" type="user" ><!-- 列名id_,username_,birthday_id:要映射結(jié)果集(查詢的結(jié)果select id id_,username username_,birthday birthday_)的唯 一標識 ,稱為主鍵column:結(jié)果集(查詢的結(jié)果)的列名property:type指定的哪個屬性中--><id column="id_" property="id"/><!-- result就是普通列的映射配置 --><result column="username_" property="username"/><result column="birthday_" property="birthday"/></resultMap><!-- 根據(jù)id查詢用戶信息 --><!-- id:唯一標識 一個statement#{}:表示 一個占位符,如果#{}中傳入簡單類型的參數(shù),#{}中的名稱隨意parameterType:輸入 參數(shù)的類型,通過#{}接收parameterType輸入 的參數(shù)resultType:輸出結(jié)果 類型,不管返回是多條還是單條,指定單條記錄映射的pojo類型--><select id="findUserById" parameterType="int" resultType="user">SELECT * FROM USER WHERE id= #{id}</select><!-- 根據(jù)用戶名稱查詢用戶信息,可能返回多條${}:表示sql的拼接,通過${}接收參數(shù),將參數(shù)的內(nèi)容不加任何修飾拼接在sql中。--><select id="findUserByName" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User">select * from user where username like '%${value}%'</select><!-- 自定義查詢條件查詢用戶的信息parameterType:指定包裝類型%${userCustom.username}%:userCustom是userQueryVo中的屬性,通過OGNL獲取屬性的值--><select id="findUserList" parameterType="userQueryVo" resultType="user">select id,username,birthday from user<!-- where標簽相當 于where關(guān)鍵字,可以自動去除第一個and --><where><!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace --><include refid="query_user_where"></include><!-- 下邊還有很其它的條件 --><!-- <include refid="其它的sql片段"></include> --></where></select><!-- 使用resultMap作結(jié)果映射resultMap:如果引用resultMap的位置和resultMap的定義在同一個mapper.xml,直接使用resultMap的id,如果不在同一個mapper.xml要在resultMap的id前邊加namespace--><select id="findUserListResultMap" parameterType="userQueryVo" resultMap="userListResultMap">select id id_,username username_,birthday birthday_ from user where username like '%${userCustom.username}%'</select><!-- 輸出簡單類型功能:自定義查詢條件,返回查詢記錄個數(shù),通常用于實現(xiàn) 查詢分頁--><select id="findUserCount" parameterType="userQueryVo" resultType="int">select count(*) from user <!-- where標簽相當 于where關(guān)鍵字,可以自動去除第一個and --><where><!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace --><include refid="query_user_where"></include><!-- 下邊還有很其它的條件 --><!-- <include refid="其它的sql片段"></include> --></where></select><!-- 添加用戶parameterType:輸入 參數(shù)的類型,User對象 包括 username,birthday,sex,address#{}接收pojo數(shù)據(jù),可以使用OGNL解析出pojo的屬性值#{username}表示從parameterType中獲取pojo的屬性值selectKey:用于進行主鍵返回,定義了獲取主鍵值的sqlorder:設(shè)置selectKey中sql執(zhí)行的順序,相對于insert語句來說keyProperty:將主鍵值設(shè)置到哪個屬性resultType:select LAST_INSERT_ID()的結(jié)果 類型--><insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"><selectKey keyProperty="id" order="AFTER" resultType="int">select LAST_INSERT_ID()</selectKey>INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})</insert><!-- mysql的uuid生成主鍵 --><!-- <insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"><selectKey keyProperty="id" order="BEFORE" resultType="string">select uuid()</selectKey>INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})</insert> --><!-- oracle在執(zhí)行insert之前執(zhí)行select 序列.nextval() from dual取出序列最大值,將值設(shè)置到user對象 的id屬性--><!-- <insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"><selectKey keyProperty="id" order="BEFORE" resultType="int">select 序列.nextval() from dual</selectKey>INSERT INTO USER(id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})</insert> --><!-- 用戶刪除 --><delete id="deleteUser" parameterType="int">delete from user where id=#{id}</delete><!-- 用戶更新 要求:傳入的user對象中包括 id屬性值--><update id="updateUser" parameterType="cn.itcast.mybatis.po.User">update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}</update></mapper>測試代碼:
package cn.itcast.mybatis.mapper;import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List;import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test;import cn.itcast.mybatis.po.User; import cn.itcast.mybatis.po.UserCustom; import cn.itcast.mybatis.po.UserQueryVo;public class UserMapperTest {// 會話工廠private SqlSessionFactory sqlSessionFactory;// 創(chuàng)建工廠@Beforepublic void init() throws IOException {// 配置文件(SqlMapConfig.xml)String resource = "SqlMapConfig.xml";// 加載配置文件到輸入 流InputStream inputStream = Resources.getResourceAsStream(resource);// 創(chuàng)建會話工廠sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testFindUserById() throws Exception {SqlSession sqlSession = sqlSessionFactory.openSession();// 創(chuàng)建代理對象,接口實現(xiàn)類UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.findUserById(1);System.out.println(user);}@Testpublic void testFindUserByUsername() throws Exception {SqlSession sqlSession = sqlSessionFactory.openSession();// 創(chuàng)建代理對象,接口實現(xiàn)類UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User> list = userMapper.findUserByName("小明");System.out.println(list);}@Testpublic void testInsertUser() throws Exception {SqlSession sqlSession = sqlSessionFactory.openSession();// 創(chuàng)建代理對象,接口實現(xiàn)類UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 插入對象User user = new User();user.setUsername("李奎");userMapper.insertUser(user);sqlSession.commit();sqlSession.close();}// 通過包裝類型查詢用戶信息@Testpublic void testFindUserList() throws Exception {SqlSession sqlSession = sqlSessionFactory.openSession();// 創(chuàng)建代理對象,接口實現(xiàn)類UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 構(gòu)造查詢條件UserQueryVo userQueryVo = new UserQueryVo();UserCustom userCustom = new UserCustom();userCustom.setUsername("小明");userCustom.setSex("1");userQueryVo.setUserCustom(userCustom);//id集合List<Integer> ids = new ArrayList<Integer>();ids.add(16);ids.add(22);userQueryVo.setIds(ids);List<User> list = userMapper.findUserList(userQueryVo);sqlSession.close();System.out.println(list);}// 使用resultMap進行結(jié)果映射 @Testpublic void testFindUserListResultMap() throws Exception {SqlSession sqlSession = sqlSessionFactory.openSession();// 創(chuàng)建代理對象,接口實現(xiàn)類UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 構(gòu)造查詢條件UserQueryVo userQueryVo = new UserQueryVo();UserCustom userCustom = new UserCustom();userCustom.setUsername("小明");userQueryVo.setUserCustom(userCustom);List<User> list = userMapper.findUserListResultMap(userQueryVo);sqlSession.close();System.out.println(list);}// 返回查詢記錄總數(shù)@Testpublic void testFindUserCount() throws Exception {SqlSession sqlSession = sqlSessionFactory.openSession();// 創(chuàng)建代理對象,接口實現(xiàn)類UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 構(gòu)造查詢條件UserQueryVo userQueryVo = new UserQueryVo();UserCustom userCustom = new UserCustom();userCustom.setUsername("小明");userQueryVo.setUserCustom(userCustom);int count = userMapper.findUserCount(userQueryVo);sqlSession.close();System.out.println(count);}}UserQueryVo.java
package cn.itcast.mybatis.po;import java.util.List;/** 包裝類型,將來在使用時從頁面?zhèn)鞯絚ontroller、service、mapper </p>*/ public class UserQueryVo {//用戶信息private User user;//自定義user的擴展對象private UserCustom userCustom;//用戶id集合private List<Integer> ids;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public UserCustom getUserCustom() {return userCustom;}public void setUserCustom(UserCustom userCustom) {this.userCustom = userCustom;}public List<Integer> getIds() {return ids;}public void setIds(List<Integer> ids) {this.ids = ids;}}?
轉(zhuǎn)載于:https://www.cnblogs.com/yaowen/p/4869990.html
總結(jié)
以上是生活随笔為你收集整理的mybatis15 mapper方式 代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建自已的sql函数
- 下一篇: flask框架(三):flask配置文件