这一篇讲述MyBatis对数据库的CRUD操作,内容不做重复,只做添加:查看学习总结1
一、使用MyBatis对表执行CRUD操作——基于XML的实现
在SQL映射文件userMapper.xml中添加
<!-- 增加用户 --> <insert id="addUser" parameterType="com.tfj.domain.User"> insert into users(name,age) values(#{name},#{age}) </insert> <!-- 删除用户 --> <delete id="deleteUserById" parameterType="int"> delete from users where id=#{id} </delete> <!-- 修改用户 --> <update id="updateUserById" parameterType="com.tfj.domain.User"> update users set name=#{name},age=#{age} where id=#{id} </update> <!-- 获取全部用户 --> <select id="getAllUsers" resultType="com.tfj.domain.User"> select * from users </select>
映射封装类->CRUDByXmlMapper.java
package com.tfj.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.tfj.domain.User; import com.tfj.util.MyBatisUtil; public class CRUDByXmlMapper { public void insert() { // SqlSession sqlSession = MyBatisUtil.getSqlSession(false); SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映射sql的标识字符串, com.tfj.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * addUser是insert标签的id属性值,通过insert标签的id属性值就可以找到要执行的SQL */ String statement = "com.tfj.mapping.userMapper.addUser";// 映射sql的标识字符串 User user = new User(); user.setName("AfterInsert"); user.setAge(20); // 执行插入操作 int retResult = sqlSession.insert(statement, user); // 手动提交事务 // sqlSession.commit(); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } public void update() { SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映射sql的标识字符串, com.tfj.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * updateUser是update标签的id属性值,通过update标签的id属性值就可以找到要执行的SQL */ String statement = "com.tfj.mapping.userMapper.updateUserById";// 映射sql的标识字符串 User user = new User(); user.setId(1); user.setName("AfterUpdate"); user.setAge(25); // 执行修改操作 int retResult = sqlSession.update(statement, user); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } public void delete() { SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映射sql的标识字符串, com.tfj.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * deleteUser是delete标签的id属性值,通过delete标签的id属性值就可以找到要执行的SQL */ String statement = "com.tfj.mapping.userMapper.deleteUserById";// 映射sql的标识字符串 // 执行删除操作 int retResult = sqlSession.delete(statement, 1); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } public void select() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映射sql的标识字符串, com.tfj.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getAllUsers是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.tfj.mapping.userMapper.getUserById";// 映射sql的标识字符串 // 执行查询返回一个唯一user对象的sql User user = sqlSession.selectOne(statement, 1); System.out.println(user); } public void selectAll() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映射sql的标识字符串, com.tfj.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getAllUsers是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.tfj.mapping.userMapper.getAllUsers";// 映射sql的标识字符串 // 执行查询操作,将查询结果自动封装成List<User>返回 List<User> lstUsers = sqlSession.selectList(statement); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close();
for(User lsts:lstUsers){ System.out.println(lsts); }
} }
工具类(后续将一直使用)->MyBatisUtil.java
package com.tfj.util; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { /** * 获取SqlSessionFactory * * @return SqlSessionFactory */ public static SqlSessionFactory getSqlSessionFactory() { String resource = "conf.xml"; InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); return factory; } /** * 获取SqlSession * * @return SqlSession */ public static SqlSession getSqlSession() { return getSqlSessionFactory().openSession(); } /** * 获取SqlSession * * @param isAutoCommit * true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 false * 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务 * @return SqlSession */ public static SqlSession getSqlSession(boolean isAutoCommit) { return getSqlSessionFactory().openSession(isAutoCommit); } }
测试类->CRUDTest.java
package com.tfj.test; public class CRUDTest { public static void main(String[] args) { // 每次进行操作后都查询全部用户,从而观察变化 CRUDByXmlMapper testCRUDByXmlMapper = new CRUDByXmlMapper(); System.out.println("-------------------------"); System.out.println("Look getAllUsers"); testCRUDByXmlMapper.selectAll(); System.out.println("-------------------------"); System.out.println("Look after updateUser"); testCRUDByXmlMapper.update(); testCRUDByXmlMapper.selectAll(); System.out.println("-------------------------"); System.out.println("Look after delete"); testCRUDByXmlMapper.delete(); testCRUDByXmlMapper.selectAll(); System.out.println("-------------------------"); System.out.println("Look after insert"); testCRUDByXmlMapper.insert(); testCRUDByXmlMapper.selectAll(); } }
二、使用MyBatis对表执行CRUD操作——基于注解的实现
1、定义SQL映射的接口
UserMapperI接口的代码如下:
UserMapperI.java
package com.tfj.mapping; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.tfj.domain.User; /** * @author gacl 定义sql映射的接口,使用注解指明方法要执行的SQL */ public interface UserMapperI { // 使用@Insert注解指明insert方法要执行的SQL @Insert("insert into users(name, age) values(#{name}, #{age})") public int insert(User user); // 使用@Delete注解指明delete方法要执行的SQL @Delete("delete from users where id=#{id}") public int delete(int id); // 使用@Update注解指明update方法要执行的SQL @Update("update users set name=#{name},age=#{age} where id=#{id}") public int update(User user); // 使用@Select注解指明select方法要执行的SQL @Select("select * from users where id=#{id}") public User select(int id); // 使用@Select注解指明selectAll方法要执行的SQL @Select("select * from users") public List<User> selectAll(); }
在配置文件conf.xml中添加这个注解接口
<!-- 注册UserMapper映射接口 --> <mapper class="com.tfj.mapping.UserMapperI" />
映射封装类->CRUDByAnnotationMapper.java
package com.tfj.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.tfj.domain.User; import com.tfj.mapping.UserMapperI; import com.tfj.util.MyBatisUtil; public class CRUDByAnnotationMapper { public void insert() { SqlSession sqlSession = MyBatisUtil.getSqlSession(true); // 得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 UserMapperI mapper = sqlSession.getMapper(UserMapperI.class); User user = new User(); user.setName("tufujie"); user.setAge(20); int add = mapper.insert(user); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(add); } public void update() { SqlSession sqlSession = MyBatisUtil.getSqlSession(true); // 得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 UserMapperI mapper = sqlSession.getMapper(UserMapperI.class); User user = new User(); user.setId(3); user.setName("Jacksile"); user.setAge(26); // 执行修改操作 int retResult = mapper.update(user); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } public void delete() { SqlSession sqlSession = MyBatisUtil.getSqlSession(true); // 得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 UserMapperI mapper = sqlSession.getMapper(UserMapperI.class); // 执行删除操作 int retResult = mapper.delete(1); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } public void select() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); // 得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 UserMapperI mapper = sqlSession.getMapper(UserMapperI.class); // 执行查询操作,将查询结果自动封装成User返回 User user = mapper.select(2); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(user); } public void selectAll() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); // 得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 UserMapperI mapper = sqlSession.getMapper(UserMapperI.class); // 执行查询操作,将查询结果自动封装成List<User>返回 List<User> lstUsers = mapper.selectAll(); // 使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close();
for(User lsts:lstUsers){ System.out.println(lsts); }
} }
测试类->CRUDTest2.java
package com.tfj.test; public class CRUDTest2 { public static void main(String[] args) { // 每次进行操作后都查询全部用户,从而观察变化 CRUDByAnnotationMapper testCRUDByXmlMapper = new CRUDByAnnotationMapper(); System.out.println("-------------------------"); System.out.println("Look after getUser id=2"); testCRUDByXmlMapper.select(); System.out.println("Look after getAllUsers"); testCRUDByXmlMapper.selectAll(); System.out.println("-------------------------"); System.out.println("Look after updateUser"); testCRUDByXmlMapper.update(); testCRUDByXmlMapper.selectAll(); System.out.println("-------------------------"); System.out.println("Look after delete"); testCRUDByXmlMapper.delete(); testCRUDByXmlMapper.selectAll(); System.out.println("-------------------------"); System.out.println("Look after insert"); testCRUDByXmlMapper.insert(); testCRUDByXmlMapper.selectAll(); } }