有部分相同不需要修改可以点击--->简单的Mybatis项目
1.配置Maven依赖
2.配置环境文件mybatis-config.xml
3.编写MybatisUtils工具类
4.编写实体类User
5.实体类UserMapperr的接口 (在小进阶后 实体类名字修改为了UserMapper)
1 package com.zxy.dao; 2 3 import com.zxy.pojo.User; 4 5 import java.util.List; 6 7 public interface UserMapper { 8 //获取全部用户 9 List<User> getUserList(); 10 11 //根据ID查询用户 12 User getUserById(int id); 13 14 //insert一个用户 15 int addUser(User user); 16 17 //修改用户 18 int updateUser(User user); 19 20 //删除一个用户 21 int deleteUser(int id); 22 }
6.编写基于 XML的对于sql的映射语句UserMapper.xml
增删改不需要返回结果类型
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 7 <mapper namespace="com.zxy.dao.UserMapper"> 8 <select id="getUserList" resultType="com.zxy.pojo.User" > 9 select * from mybatis.user; 10 </select> 11 //查询 12 <select id="getUserById" parameterType="int" resultType="com.zxy.pojo.User"> 13 select * from mybatis.user where id = #{id}; 14 </select> 15 //增加 16 <insert id="addUser" parameterType="com.zxy.pojo.User"> 17 insert into mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd}) 18 </insert> 19 //修改 20 <update id="updateUser" parameterType="com.zxy.pojo.User"> 21 update mybatis.user set name =#{name},pwd=#{pwd} where id = #{id}; 22 </update> 23 //删除 24 <delete id="deleteUser" parameterType="int"> 25 delete from mybatis.user 26 where id = #{id}; 27 </delete> 28 </mapper>
7.编写测试类
注意!!!:增删改是需要提交事务 !!!
有两种方法 1.如下面所示 手动提交 sqlSession.commit();
2.在工具类MybatisUtils中
return sqlSessionFactory.openSession(true);
在括号中传值true 可以设置为自动提交
1.查询
1 public void test(){ 2 3 //1.获取sqlSession对象 4 SqlSession sqlSession = MybatisUtils.getSqlSession(); 5 //2. 6 UserMapper userDao = sqlSession.getMapper(UserMapper.class); 7 List<User> userList = userDao.getUserList(); 8 for ( User user :userList) { 9 System.out.println(user); 10 } 11 12 //关闭SqlSession 13 sqlSession.close(); 14 } 15 16 @Test 17 public void test2(){ 18 SqlSession sqlSession = MybatisUtils.getSqlSession(); 19 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 20 User user = mapper.getUserById(1); 21 System.out.println(user); 22 sqlSession.close(); 23 }
2.增加
1 @Test 2 public void test3(){ 3 SqlSession sqlSession = MybatisUtils.getSqlSession(); 4 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 5 int rs = mapper.addUser(new User(4,"爱德华","44451313")); 6 if(rs>0){ 7 System.out.println("插入成功"); 8 } 9 //提价事务 10 sqlSession.commit(); 11 sqlSession.close(); 12 }
3.修改
1 @Test 2 public void test4() { 3 SqlSession sqlSession = MybatisUtils.getSqlSession(); 4 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 5 mapper.updateUser(new User(4,"呵呵","999")); 6 sqlSession.commit(); 7 sqlSession.close(); 8 }
4.删除
1 @Test 2 public void test5() { 3 SqlSession sqlSession = MybatisUtils.getSqlSession(); 4 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 5 mapper.deleteUser(4); 6 sqlSession.commit(); 7 sqlSession.close(); 8 }
标签:mapper,进阶,改查,UserMapper,sqlSession,user,Mybatis,id,User From: https://www.cnblogs.com/kidzxy/p/16797379.html