CSDN话题挑战赛第2期
参赛话题:学习笔记
目录
1. MyBatis流程回顾
上一篇中我们已经了解了MyBatis框架的基本实现,现在先来回顾一下其实现流程
2. 工具类的构建
观察上述步骤,我们发现每次都需要通过
SqlSessionFactoryBuilder
构建
SqlSessionFactory
然后再获得
SqlSession
对象,这些步骤都是相同的,我们可以把它们封装成为一个工具类
MybatisUtils
packagecom.zxy.mybatis.Utils;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importjava.io.IOException;importjava.io.InputStream;publicclassMybatisUtils{publicstaticSqlSessiongetSqlSession()throwsIOException{//读取MyBatis的核心配置文件InputStream is =Resources.getResourceAsStream("mybatis-config.xml");//创建SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder =newSqlSessionFactoryBuilder();//通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory,生产SqlSession对象SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);//创建SqlSession对象,此时通过SqlSession对象所操作的sql都必须手动提交或回滚事务//SqlSession sqlSession = sqlSessionFactory.openSession();//创建SqlSession对象,此时通过SqlSession对象所操作的sql都会自动提交SqlSession sqlSession = sqlSessionFactory.openSession(true);return sqlSession;}}
3. 相关各类配置
- 实体类User
packagecom.zxy.mybatis.pojo;publicclassUser{privateInteger id;privateString username;privateString psd;privateInteger age;privateString gender;privateString email;publicUser(){}publicUser(Integer id,String username,String psd,Integer age,String gender,String email){this.id = id;this.username = username;this.psd= psd;this.age = age;this.gender = gender;this.email = email;}@OverridepublicStringtoString(){return"User{"+"id="+ id +", username='"+ username +'\''+", psd='"+ psd+'\''+", age="+ age +", gender='"+ gender +'\''+", email='"+ email +'\''+'}';}publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetUsername(){return username;}publicvoidsetUsername(String username){this.username = username;}publicStringgetPsd(){return psd;}publicvoidsetPsd(String psd){this.psd= psd;}publicIntegergetAge(){return age;}publicvoidsetAge(Integer age){this.age = age;}publicStringgetGender(){return gender;}publicvoidsetGender(String gender){this.gender = gender;}publicStringgetEmail(){return email;}publicvoidsetEmail(String email){this.email = email;}}
- UserMapper
packagecom.zxy.mybatis.mapper;importcom.zxy.mybatis.pojo.User;importjava.util.List;publicinterfaceUserMapper{/**
* 添加用户信息
*/intinsertUser();intdeleteUser();intupdateUser();UsergetUserById();}
- 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 namespace="com.zxy.mybatis.mapper.UserMapper"><!--intinsertUser();--><insert id="insertUser">
insert into t_user values(null,'bobo','123456',23,'男','[email protected]')</insert><!--intdeleteUser();--><delete id="deleteUser">
delete from t_user where id =1</delete><!--intupdateUser();--><update id="updateUser">
update t_user set username='zxy',password='123' where id =6</update><!--UsergetUserById();--><select id="getUserById" resultType="com.zxy.mybatis.pojo.User">
select * from t_user where id =1</select></mapper>
4. 增加数据功能
首先在UserMapper中写入方法
intinsertUser();
配置Mapper接口
<!--int insertUser();--><insertid="insertUser">
insert into t_user values(null,'bobo','123456',23,'男','[email protected]')
</insert>
执行方法测试
查看结果
返回值表明已经成功,我们查看数据库进一步确认
加入成功
5. 删除数据功能
首先在UserMapper中写入方法
配置Mapper接口
<!--int deleteUser();--><deleteid="deleteUser">
delete from t_user where id = 9
</delete>
执行方法测试
结果如下
查看数据库是否成功
可以发现id为9的已经被删除
6. 修改数据功能
首先在UserMapper中写入方法
配置UserMapper
<!--int updateUser();--><updateid="updateUser">
update t_user set username='zxy',password='123' where id = 7
</update>
执行方法测试
结果如下
查看数据库
id为6的那个数据确实已经发生改变
7. 查询数据功能
首先在UserMapper中写入方法
配置UserMapper
<!--User getUserById();--><selectid="getUserById"resultType="com.zxy.mybatis.pojo.User">
select * from t_user where id = 7
</select>
执行方法测试
结果如下,表明查询成功
版权归原作者 不遇言熙 所有, 如有侵权,请联系我们删除。