文章目录

  • Mybatis创建流程总结
    • 1.mybatis核心配置文件
    • 2.配置数据库连接配置文件
    • 3.写生成SQLSession对象
    • 4.测试用例的设置
    • 5.综合练习
      • (1) BillDao
      • (2) ProviderDao
      • (3) RoleDao
      • (4) UserDao

Mybatis创建流程总结

1.mybatis核心配置文件

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration><!--引入外部文件--><properties resource="db.properties" /><!--设置一些内容,如:日志--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><!--设置别名,设置一个较短的名字--><typeAliases><!--1.可以减少类完全限定名的冗余--><!--   <typeAlias type="entity.User" alias="User"></typeAlias>--><!--可以设置包名,此时引用的类名是类的小写--><package name="entity"></package></typeAliases><!--这是数据库驱动配置--><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册--><!--绑定接口--><mappers><mapper class="dao.BlogMapper"/></mappers>
</configuration>

在这一核心配置文件中,有如下标签(有严格的先后顺序):properties,settings,typeAliases,typeHandlers,objectFactory,objectWrapperFactory,reflectorFactory,plugins,environments,databaseIdProvider,mappers

2.配置数据库连接配置文件

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8&serverTimezone=UTC&useUnicode=true
username=root
password=root

3.写生成SQLSession对象

public class MybatisUtil {private static SqlSessionFactory sqlSessionFactory;static {try {//使用Mybatis第一步:获取sqlSessionFactory对象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}//既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。// SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。public static SqlSession getSqlSession() {return sqlSessionFactory.openSession(true);}
}

4.设立实体类

5.设立Mapper接口与接口对应的Mapper.xml

Mapper.java(接口)

public interface BlogMapper {int addBlog(Blog blog);//查询博客List<Blog> findBlogByIF(Map map);//根据title或author属属性查询List<Blog> findBlogByChoose(Map map);//更新一条信息void updateBlogById(Map map);
}

Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件,namespace中确定与那个java接口对接-->
<mapper namespace="dao.BlogMapper"><!--id是方法名,parameterType是参数类型,resultType是返回值类型--><insert id="addBlog" parameterType="blog">insert into mybatis.blog(id, title, author, create_time, views)values(#{id}, #{title}, #{author}, #{createTime}, #{views});</insert><select id="findBlogByIF" parameterType="map" resultType="blog">select * from mybatis.blog where 1=1<if test="title != null">and title = #{title}</if><if test="author != null">and author = #{author}</if></select><select id="findBlogByChoose" parameterType="map" resultType="blog">select * from mybatis.blog<where><choose><when test="title != null">and title = #{title}</when><when test="author != null">and author = #{author}</when><otherwise>1=1</otherwise></choose></where></select><update id="updateBlogById" parameterType="map">update mybatis.blog<set><if test="title != null">title = #{title}</if><if test="author != null">author = #{author}</if></set><where><if test="id != null">id = #{id}</if><if test="views != null">views = #{views}</if></where></update>
</mapper>

4.测试用例的设置

public class BlogTest {@Testpublic void addaBlogTest(){SqlSession sqlSession = MybatisUtil.getSqlSession();BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);Blog blog = new Blog();blog.setId(IDUtil.getId());blog.setAuthor("ffideal");blog.setTitle("跟着大佬学SSM");blog.setCreateTime(new Date());blog.setViews(10086);mapper.addBlog(blog);sqlSession.close();}@Testpublic void testIF(){SqlSession sqlSession = MybatisUtil.getSqlSession();BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);Map<Object,Object> map = new HashMap<>();map.put("title","SSM");map.put("author","ffideal");List<Blog> blogs = mapper.findBlogByIF(map);for(Blog blog : blogs){System.out.println(blog);}sqlSession.close();}@Testpublic void testChoose(){SqlSession sqlSession = MybatisUtil.getSqlSession();BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);HashMap<Object, Object> map = new HashMap<>();map.put("title","SSM");mapper.findBlogByChoose(map);sqlSession.close();}@Testpublic void testWhere(){SqlSession sqlSession = MybatisUtil.getSqlSession();BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);HashMap<Object, Object> map = new HashMap<>();map.put("title","test");map.put("id","160e85c2343c4c7ea19add8662b7b4dd");mapper.updateBlogById(map);sqlSession.close();}
}

5.综合练习

配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration><!--引入外部文件--><properties resource="database.properties" /><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><!--设置别名,设置一个较短的名字--><typeAliases><!--1.可以减少类完全限定名的冗余--><!--   <typeAlias type="entity.User" alias="User"></typeAlias>--><!--可以设置包名,此时引用的类名是类的小写--><package name="entity"></package><package name="dao"></package></typeAliases><!--这是数据库驱动配置--><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册--><!--绑定接口--><mappers><mapper class="dao.bill.BillDao"/><mapper class="dao.provider.ProviderDao"/><mapper class="dao.role.RoleDao"/><mapper class="dao.user.UserDao"/></mappers>
</configuration>

建立会话工厂

public class MybatisUtil {private static SqlSessionFactory sqlSessionFactory;static {try {//使用Mybatis第一步:获取sqlSessionFactory对象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}//既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。// SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。public static SqlSession getSqlSession() {return sqlSessionFactory.openSession(true);}
(1) BillDao

BillDao.java

public interface BillDao {/*** 增加订单* @param bill* @return* @throws Exception*/public int add(Bill bill)throws Exception;/*** 通过查询条件获取供应商列表-模糊查询-getBillList* @param bill* @return* @throws Exception*/public List<Bill> getBillList(Bill bill)throws Exception;/*** 通过delId删除Bill* @param delId* @return* @throws Exception*/public int deleteBillById(String delId)throws Exception;/*** 通过billId获取Bill* @param id* @return* @throws Exception*/public Bill getBillById(String id)throws Exception;/*** 修改订单信息* @param bill* @return* @throws Exception*/public int modify(Bill bill)throws Exception;/*** 根据供应商ID查询订单数量* @param providerId* @return* @throws Exception*/public int getBillCountByProviderId(String providerId)throws Exception;}

BillDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="dao.bill.BillDao"><insert id="add" parameterType="bill">INSERT INTO smbms.smbms_bill (id, billCode, productName, productDesc, productUnit, productCount, totalPrice, isPayment, createdBy, creationDate, modifyBy, modifyDate, providerId)VALUES (#{id}, #{billCode}, #{productName}, #{productDesc}, #{productUnit}, #{productCount}, #{totalPrice}, #{isPayment}, #{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate}, #{providerId});</insert><!--通过查询条件获取供应商列表-模糊查询-可以通过名称、类别、价格来做模糊查询。使用choose/when/otherwise-->
<!--    <select id="getBillList" parameterType="bill" resultType="bill">select * from smbms.smbms_bill<where><if test="productName != null">and productName like "%"#{productName}"%"</if><if test="productDesc != null">and productDesc like "%"#{productDesc}"%"</if><if test="productCount != null">and productCount like "%"#{productCount}"%"</if></where></select>--><select id="getBillList" parameterType="bill" resultType="bill">select * from smbms.smbms_bill<where><if test="productName != null">and productName like CONCAT('%',#{productName},'%')</if><if test="productDesc != null">and productDesc like CONCAT('%',#{productDesc},'%')</if><if test="productCount != null">and productCount like CONCAT('%',#{productCount},'%')</if></where></select><!--通过delId删除Bill--><delete id="deleteBillById" parameterType="String">delete from smbms.smbms_bill<where>id = #{id}</where></delete><!--通过billId获取Bill--><select id="getBillById" parameterType="String" resultType="bill">select * from smbms.smbms_bill<where>id = #{id}</where></select><!--修改订单信息--><update id="modify" parameterType="bill">update smbms.smbms_bill<set><if test="productName != null">productName = #{productName},</if><if test="productDesc != null">productDesc = #{productDesc},</if><if test="productUnit != null">productUnit = #{productUnit},</if><if test="productCount != null">productCount = #{productCount},</if></set><where>id = #{id}</where></update><!--根据供应商ID查询订单数量--><select id="getBillCountByProviderId" parameterType="String" resultType="int">select count(*) from smbms.smbms_bill<where>providerId = #{providerId}</where></select>
</mapper>

BillDaoTest.java

package dao.bill;import Utils.MybatisUtil;
import entity.Bill;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.math.BigDecimal;
import java.util.Date;
import java.util.List;/*** @ClassName: BillDaoTest* @description:* @author: FFIDEAL* @Date: 2020/5/31 10:53*/public class BillDaoTest {@Testpublic void testAdd() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();BillDao mapper = sqlSession.getMapper(BillDao.class);Bill bill = new Bill();bill.setId(18);bill.setBillCode("BILL2016_018");bill.setProductName("娃哈哈");bill.setProductDesc("饮料");bill.setProductUnit("瓶");bill.setProductCount(BigDecimal.valueOf(2000.00));bill.setTotalPrice(BigDecimal.valueOf(4000.00));bill.setIsPayment(2);bill.setCreatedBy(1);bill.setCreationDate(new Date());bill.setProviderId(2);mapper.add(bill);sqlSession.close();}/*测试getBillList*/@Testpublic void testGetBillList() throws  Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();BillDao mapper = sqlSession.getMapper(BillDao.class);Bill bill = new Bill();//bill.setProductCount(BigDecimal.valueOf(2000.00));bill.setProductName("油");List<Bill> billList = mapper.getBillList(bill);for (Bill bill1 : billList) {System.out.println(bill1);}sqlSession.close();}/*测试deleteBillById*/@Testpublic void testDeleteBillById() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();BillDao mapper = sqlSession.getMapper(BillDao.class);mapper.deleteBillById("18");sqlSession.close();}/*通过billId获取Bill*/@Testpublic void testGetBillById() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();BillDao mapper = sqlSession.getMapper(BillDao.class);mapper.getBillById("17");sqlSession.close();}/*修改订单信息*/@Testpublic void testModify() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();BillDao mapper = sqlSession.getMapper(BillDao.class);Bill bill = new Bill();bill.setId(12);bill.setProductName("修改订单信息");mapper.modify(bill);sqlSession.close();}/*根据供应商ID查询订单数量*/@Testpublic void testGetBillCountByProviderId() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();BillDao mapper = sqlSession.getMapper(BillDao.class);int billCountByProviderId = mapper.getBillCountByProviderId("14");System.out.println("一共有" + billCountByProviderId + "条。");sqlSession.close();}
}
(2) ProviderDao

ProviderDao.java

public interface ProviderDao {/*** 增加供应商* @param provider* @return* @throws Exception*/public int add(Provider provider)throws Exception;/*** 通过供应商名称、编码获取供应商列表-模糊查询-providerList* @param proName* @return* @throws Exception*/public List<Provider> getProviderList(@Param("proName") String proName, @Param("proCode")String proCode)throws Exception;/*** 通过proId删除Provider* @param delId* @return* @throws Exception*/public int deleteProviderById( String delId)throws Exception;/*** 通过proId获取Provider* @param id* @return* @throws Exception*/public Provider getProviderById(String id)throws Exception;/*** 修改用户信息* @param user* @return* @throws Exception*/public int modify(Provider provider)throws Exception;}

ProviderDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="dao.provider.ProviderDao"><!--增加供应商--><insert id="add" parameterType="provider">insert into smbms.smbms_provider(id, proCode, proName, proDesc, proContact, proPhone, proAddress, proFax, createdBy, creationDate, modifyDate, modifyBy)values(#{id}, #{proCode}, #{proName}, #{proDesc}, #{proContact}, #{proPhone}, #{proAddress}, #{proFax}, #{createdBy}, #{creationDate}, #{modifyDate}, #{modifyBy} )</insert><!--通过供应商名称、编码获取供应商列表-模糊查询-providerList--><select id="getProviderList" parameterType="String" resultType="provider">select * from smbms.smbms_provider<where><if test="proName != null">and proName like CONCAT('%',#{proName},'%')</if><if test="proCode != null">and proCode like CONCAT('%',#{proCode},'%')</if></where></select><!--通过proId删除Provider--><delete id="deleteProviderById" parameterType="String">delete from smbms.smbms_provider<where>id = #{id}</where></delete><!--通过proId获取Provider--><select id="getProviderById" parameterType="String" resultType="provider">select * from smbms.smbms_provider<where>id = #{id}</where></select><!--修改用户信息proCode, proName, proDesc, proContact, proPhone--><update id="modify" parameterType="provider">update smbms.smbms_provider<set><if test="proCode != null">proCode = #{proCode},</if><if test="proName != null">proName = #{proName},</if><if test="proDesc != null">proDesc = #{proDesc},</if><if test="proContact">proContact = #{proContact},</if><if test="proPhone">proPhone = #{proPhone},</if></set><where>id = #{id}</where></update>
</mapper>

ProviderDaoTest.java

package dao.provider;import Utils.MybatisUtil;
import entity.Provider;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.Date;
import java.util.List;public class ProviderDaoTest {@Testpublic void testAdd() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();ProviderDao mapper = sqlSession.getMapper(ProviderDao.class);Provider provider = new Provider();provider.setId(15);provider.setProCode("ZJ_GYS002");provider.setProName("乐摆日用品厂");provider.setProDesc("长期合作伙伴,主营产品:各种中、高档塑料杯,塑料乐扣水杯(密封杯)、保鲜杯(保鲜盒)、广告杯、礼品杯");provider.setProContact("王世杰");provider.setProPhone("13212331567");provider.setProAddress("浙江省金华市义乌市义东路");provider.setProFax("0579-34452321");provider.setCreatedBy(1);provider.setCreationDate(new Date());mapper.add(provider);sqlSession.close();}@Testpublic void testGetProviderList() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();ProviderDao mapper = sqlSession.getMapper(ProviderDao.class);List<Provider> providerList = mapper.getProviderList("厂", "GZ");for (Provider provider : providerList) {System.out.println(provider);}sqlSession.close();}@Testpublic void testDeleteProviderById() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();ProviderDao mapper = sqlSession.getMapper(ProviderDao.class);mapper.deleteProviderById("1");sqlSession.close();}/*测试 getProviderById*/@Testpublic void testGetProviderById() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();ProviderDao mapper = sqlSession.getMapper(ProviderDao.class);Provider providerById = mapper.getProviderById("2");System.out.println(providerById);sqlSession.close();}/*测试modify*/@Testpublic void testModify() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();ProviderDao mapper = sqlSession.getMapper(ProviderDao.class);Provider provider = new Provider();provider.setId(2);provider.setProPhone("12345678903");provider.setProContact("王者q");mapper.modify(provider);sqlSession.close();}
}
(3) RoleDao

RoleDao.java

public interface RoleDao {public List<Role> getRoleList()throws Exception;}

RoleDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="dao.role.RoleDao"><!--getRoleList--><select id="getRoleList" resultType="role">select * from smbms.smbms_role</select>
</mapper>

RoleDaoTest.java

public class RoleDaoTest {@Testpublic void testGetRoleList() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();RoleDao mapper = sqlSession.getMapper(RoleDao.class);List<Role> roleList = mapper.getRoleList();for (Role role : roleList) {System.out.println(role);}sqlSession.close();}
}
(4) UserDao

UserDao.java

public interface UserDao {/*** 增加用户信息* @param user* @return* @throws Exception*/public int add(User user)throws Exception;/*** 通过userCode获取User* @param userCode* @return* @throws Exception*/public User getLoginUser(String userCode)throws Exception;/*** 通过条件查询-userList* @param userName* @param userRole* @return* @throws Exception*/public List<User> getUserList(@Param("userName") String userName, @Param("userRole") int userRole, @Param("currentPageNo")int currentPageNo, @Param("pageSize")int pageSize)throws Exception;/*** 通过条件查询-用户表记录数* @param userName* @param userRole* @return* @throws Exception*/public int getUserCount(@Param("userName") String userName, @Param("userRole")int userRole)throws Exception;/*** 通过userId删除user* @param delId* @return* @throws Exception*/public int deleteUserById(Integer delId)throws Exception;/*** 通过userId获取user* @param id* @return* @throws Exception*/public User getUserById(int id)throws Exception;/*** 修改用户信息* @param user* @return* @throws Exception*/public int modify( User user)throws Exception;/*** 修改当前用户密码* @param id* @param pwd* @return* @throws Exception*/public int updatePwd(@Param("id") int id, @Param("userPassword") String userPassword)throws Exception;}

UserDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="dao.user.UserDao"><!--增加用户信息  User--><insert id="add" parameterType="user">insert into smbms.smbms_user(id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate)values (#{id}, #{userCode}, #{userName}, #{userPassword}, #{gender}, #{birthday}, #{phone}, #{address}, #{userRole}, #{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate})</insert><!--通过userCode获取User--><select id="getLoginUser" parameterType="String" resultType="user">select * from smbms.smbms_user<where>userCode = #{userCode}</where></select><!--通过条件查询-userListString userName, int userRole,int currentPageNo,int pageSize--><select id="getUserList" parameterType="String" resultType="user">select * from smbms.smbms_user<where><if test="userName != null">and userName = #{userName}</if><if test="userRole != null">and userRole = #{userRole}</if></where>limit #{currentPageNo},#{pageSize}</select><!--通过条件查询-用户表记录数 String userName, int userRole--><select id="getUserCount" parameterType="String" resultType="int">select count(*) from smbms.smbms_user<where>userName = #{userName} and userRole = #{userRole}</where></select><!--通过userId删除user       Integer delId--><delete id="deleteUserById" parameterType="int">delete from smbms.smbms_user<where>id = #{id}</where></delete><!--通过userId获取user   String id--><select id="getUserById" parameterType="String" resultType="user">select * from smbms.smbms_user<where>id =#{id}</where></select><!--修改用户信息   User user   set会自动去掉逗号--><update id="modify" parameterType="user">update smbms.smbms_user<set><if test="userCode != null">userCode = #{userCode},</if><if test="userName != null">userName = #{userName},</if><if test="phone != null">phone = #{phone},</if></set><where>id = #{id}</where></update><!--修改当前用户密码    int id, String pwd--><update id="updatePwd" parameterType="String">update smbms.smbms_user<set>userPassword = #{userPassword}</set><where>id = #{id}</where></update></mapper>

UserDaoTest.java

public class UserDaoTest {@Testpublic void testAdd() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);User user = new User();user.setId(16);user.setUserCode("zhouzhiruo");user.setUserName("周芷若");user.setUserPassword("1234567");user.setGender(1);user.setBirthday(new Date());user.setPhone("12345678900");user.setAddress("北京市昌平区天通苑3区12号楼");user.setUserRole(2);user.setCreatedBy(1);user.setCreationDate(new Date());mapper.add(user);sqlSession.close();}@Testpublic void testGetLoginUser() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);User sunlei = mapper.getLoginUser("sunlei");System.out.println(sunlei);sqlSession.close();}@Testpublic void testGgetUserList() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);List<User> userList = mapper.getUserList("周芷若", 2, 0, 4);for (User user : userList) {System.out.println(user);}sqlSession.close();}@Testpublic void testGetUserCount() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);int userCount = mapper.getUserCount("赵敏", 2);System.out.println(userCount);sqlSession.close();}@Testpublic void testDeleteUserById() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);mapper.deleteUserById(5);sqlSession.close();}@Testpublic void testGetUserById() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);User userById = mapper.getUserById(13);System.out.println(userById);sqlSession.close();}@Testpublic void testModify() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);User user = new User();user.setUserName("修改用户名");user.setId(7);user.setUserCode("changeUserName");mapper.modify(user);sqlSession.close();}@Testpublic void testUpdatePwd() throws Exception{SqlSession sqlSession = MybatisUtil.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);mapper.updatePwd(6,"123456");sqlSession.close();}
}

【Mybatis 之应用篇】 5_Mybatis总结(附20道练习题以及答案)相关推荐

  1. 数学速算法_小学数学必考应用题解题思路及方法大汇总(附各年级练习题及答案)...

    长按二维码关注 小学数学学习不一定要死盯着书本,学习不是照着书背记就能学好,学习是要灵活的运用.运用的越多,掌握的就越牢靠! 应用题,可以说是小学数学中的半壁江山.做不好应用题的孩子,不止是数学成绩很 ...

  2. 深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)

    上篇文章<深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)> 介绍了properties与environments, ...

  3. Java基础-SSM之mybatis快速入门篇

    Java基础-SSM之mybatis快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 其实你可能会问什么是SSM,简单的说就是spring mvc + Spring + m ...

  4. Mybatis(第二篇:联表查询)

    Mybatis(第二篇:联表查询) 目录 Mybatis(第二篇:联表查询) 一.前期 项目的搭建 1.数据库 2.IDEA项目架构搭建 2.1 pom.xml 2.2 domain包 2.2.1 D ...

  5. 就业技术书文件表格_Word格式:工程预结算工作流程图及工作表单,附20余表格...

    在很多建筑单位,因为工程预结算的重要性,负责这块工作的基本上都是老板负责人的各种亲属,外面人基本上接触不到工作,不知道大家有没有见到这种公司呢? 工程预结算 正是因为工程预结算工作对建筑企业施工效益以 ...

  6. 计算机论文指导记录范本,论文指导内容记录怎么写 3篇 论文指导记录20篇

    论文指导内容记录怎么写 3篇 论文指导记录20篇 论文指导内容记录怎么写 3篇 论文指导记录20篇精品文档,仅供参考论文指导内容记录怎么写 3篇 论文指导记录20篇指导是一个汉语词语,读音为zhdo, ...

  7. Mysql实现文章查询上一篇和下一篇功能,附sql语句?

    mysql实现文章查询上一篇和下一篇功能,附sql语句? 最近在做一个项目用到的文章查询上一篇和下一篇功能,详见:http://www.yifen5.com Mysql实现文章查询上一篇和下一篇功能, ...

  8. 紧急整理了 20 道 Spring Boot 面试题,我经常拿来面试别人

    转载自  紧急整理了 20 道 Spring Boot 面试题,我经常拿来面试别人 面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只 ...

  9. k8s相关面试问题_最常被问到的20道Kubernetes面试题

    原标题:最常被问到的20道Kubernetes面试题 近一年参与了多场面试,设计了多道面试题,觉得可以综合考察应聘人对Kubernetes的掌握情况.笔者一般会在面试前,将题目直接发给候选人,以便其有 ...

最新文章

  1. GNN教程:Weisfeiler-Leman算法!
  2. Google App Engine上的Spring MVC和REST
  3. sql如何处理null值_如何正确处理SQL中的NULL值
  4. 嵌入式 IOT 汽车 航空 AI 领域从IP到片上系统SOC信息检索网站 DR
  5. Shell 特殊字符大全
  6. long 雪花算法_Snowflake 雪花算法
  7. 简谈【自动化协议逆向工程技术的当前趋势】
  8. opencv背景抠图
  9. 仙人掌之歌——路转峰回(4)
  10. r语言做绘制精美pcoa图_PCOA分析
  11. 骗子改电脑配置信息兜售垃圾,怕上当进来学习一下
  12. HTTP请求服务器 statuscode的状态码说明 (statuscode==500)
  13. 洗扑克牌(乱数排列)(c/python略)
  14. 某缓存系统采用LRU淘汰算法,假定缓存容量为4,并且初始为空,那么在顺序访问以下数据项的时候,1、5、1、3、5、2、4、1、2,出现缓存直接命中的次数是(),最后缓存中即将准备淘汰的数据项是()
  15. 【pytest】pytest配置文件pytest.ini详解
  16. Linux驱动基础开发
  17. 【JAVA】06 封装、继承、多态 总结(初级)
  18. amd锐龙笔记本cpu怎么样_AMD锐龙R5怎么样 AMD锐龙R5配置参数
  19. 盘一盘 Python 特别篇 20 - SciPy 稀疏矩阵
  20. 国产电流传感器芯片CH701与ACS712的分析对比

热门文章

  1. This 在 C# 中的含义
  2. 关于进程和线程以及句柄
  3. 山海演武传·黄道·第一卷 雏龙惊蛰 第二十二 ~ 二十四章 真龙之剑·星墟列将...
  4. stm8s开发(八) IIC的使用:IIC主机通信!
  5. Linux platform总线(1):总体框架
  6. 八、前端开发-JavaScript 客户端存储
  7. 聚类算法(1):K-Means算法
  8. C++ Primer 5th笔记(chap 18 大型程序工具)使用命名空间成员
  9. 百度超级链XChain(1)系统架构
  10. (chap6 Http首部) 其他首部字段