3.Mybaitis的详细流程
4.CRUD
我们可以在工具类创建的时候实现自动提交事务。
设置增删改的时候们就不用手动提交事务了,会自动帮助我们提交事务
package Com.Jsxs.utils; 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 java.io.IOException; import java.io.InputStream; // SqlSessionFactory---->sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; // 第一步: 获取SqlSessionFactory对象 InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } // 既然有了sqlSessionFactory,顾名思义,我们就可以从中获得SqlSession的实列了 // sqlSession完全包含了面向数据库执行SQL命令所需要的所有方法 public static SqlSession getSqlSession(){ SqlSession sqlSession = sqlSessionFactory.openSession(true); return sqlSession; } }
设置事务的提交在工具类中
(1).通过id和姓名进行查询数据
这里的占位符号,要和Parpam(“”)里的参数相一致。
package Com.Jsxs.Dao; import Com.Jsxs.pojo.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import javax.naming.Name; import java.util.List; public interface UserMapper { @Select("select *from user") List<User> getUser(); @Select("select *from user where id=#{id} and name=#{nam}") List<User> getUserByIdName(@Param("id") int id,@Param("nam") String nam); }
如果是基本数据类型,在使用注解开发的时候我们就添加N个 @Param(“”)
(2).通过引用对象对数据进行插入(不用提交数据了)
@Insert("insert into user values(#{id},#{name},#{password})") int addUser(User user);
接口
package Com.Jsxs.Dao; import Com.Jsxs.pojo.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import javax.naming.Name; import java.util.List; public interface UserMapper { /* 进行全表信息的查询 */ @Select("select *from user") List<User> getUser(); /* 根据id 和 name 进行字段的查询 */ @Select("select *from user where id=#{id} and name=#{name}") List<User> getUserByIdName(@Param("id") int id,@Param("name") String nam); /* 通过对象的引用对数据的信息进行操作 */ @Insert("insert into user values(#{id},#{name},#{password})") int addUser(User user); }
实现
package Com.Jsxs.Dao; import Com.Jsxs.pojo.User; import Com.Jsxs.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { /** * 查询全部的用户信息 */ @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> user = mapper.getUser(); for (User user1 : user) { System.out.println(user1); } } /* * 通过ID Name进行数据的查询 */ @Test public void test_id_name(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> name = mapper.getUserByIdName(1, "吉士先生"); for (User user : name) { System.out.println(user); } } /* 随用户进行增加,通过注解的操作 */ @Test public void test_add(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.addUser(new User(7, "小时候", "123456")); if (i>0){ System.out.println("恭喜您数据进行插入成功!!"); } sqlSession.close(); } }
(3).通过引用对象对数据进行修改
@Update("update user set name=#{name},pwd=#{password} where id=#{id}") int updateUser(User user);
接口
package Com.Jsxs.Dao; import Com.Jsxs.pojo.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import javax.naming.Name; import java.util.List; public interface UserMapper { /* 进行全表信息的查询 */ @Select("select *from user") List<User> getUser(); /* 根据id 和 name 进行字段的查询 */ @Select("select *from user where id=#{id} and name=#{name}") List<User> getUserByIdName(@Param("id") int id,@Param("name") String nam); /* 通过对象的引用对数据的信息进行操作 */ @Insert("insert into user values(#{id},#{name},#{password})") int addUser(User user); /* 通过引用对象对数据进行修改 */ @Update("update user set name=#{name},pwd=#{password} where id=#{id}") int updateUser(User user); }
实现
@Test public void test_update(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.updateUser(new User(1, "是吉士先生", "11111")); if (i>0){ System.out.println("恭喜您,数据进行修改成功!!!"); } sqlSession.close(); } }
(4).通过引用对象对数据进行删除
@Delete("delete from user where id=#{id} and name=#{name} and pwd=#{password}") int deleteUser(User user);
接口
package Com.Jsxs.Dao; import Com.Jsxs.pojo.User; import org.apache.ibatis.annotations.*; import javax.naming.Name; import java.util.List; public interface UserMapper { /* 进行全表信息的查询 */ @Select("select *from user") List<User> getUser(); /* 根据id 和 name 进行字段的查询 */ @Select("select *from user where id=#{id} and name=#{name}") List<User> getUserByIdName(@Param("id") int id,@Param("name") String nam); /* 通过对象的引用对数据的信息进行操作 */ @Insert("insert into user values(#{id},#{name},#{password})") int addUser(User user); /* 通过引用对象对数据进行修改 */ @Update("update user set name=#{name},pwd=#{password} where id=#{id}") int updateUser(User user); /* 通过对象引用对数据进行删除 */ @Delete("delete from user where id=#{id} and name=#{name} and pwd=#{password}") int deleteUser(User user); }
实现
package Com.Jsxs.Dao; import Com.Jsxs.pojo.User; import Com.Jsxs.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { /** * 查询全部的用户信息 */ @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> user = mapper.getUser(); for (User user1 : user) { System.out.println(user1); } } /* * 通过ID Name进行数据的查询 */ @Test public void test_id_name(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> name = mapper.getUserByIdName(1, "吉士先生"); for (User user : name) { System.out.println(user); } } /* 随用户进行增加,通过注解的操作 */ @Test public void test_add(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.addUser(new User(7, "小时候", "123456")); if (i>0){ System.out.println("恭喜您数据进行插入成功!!"); } sqlSession.close(); } /* 对用户信息进行修改 */ @Test public void test_update(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.updateUser(new User(1, "是吉士先生", "11111")); if (i>0){ System.out.println("恭喜您,数据进行修改成功!!!"); } sqlSession.close(); } /* 对用户信息进行删除的操作 */ @Test public void test_delete(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.deleteUser(new User(7, "小时候", "123456")); if (i>0){ System.out.println("恭喜您,数据进行删除失败!!"); } } }