Mybatis简单-CRUD
CRUD
1、namespace:命名空间
namespace中的包名压要和Dao/Mapper接口包名一致
2、Select
选择,查询语句
- id:就是对应namespace中的方法名
- resultType:Sql语句执行的返回值
- parameterType:参数类型
-
编写接口
package com.lhh.dao; import com.lhh.pojo.User; import java.util.List; public interface UserMapper { //查询全部用户 List<User> getUserList(); //根据id查询 User getUserById(int id); //insert 一个用户 int addUser(User user); //修改一个用户 int updateUser(User user); //删除一个用户 int deleteUser(int id); }
-
编写对应的mapper语句
<?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.lhh.dao.UserMapper"> <!--查询全部用户--> <select id="getUserList" resultType="com.lhh.pojo.User"> select * from mybatis1.user; </select> <!--根据id查询--> <select id="getUserById" parameterType="int" resultType="com.lhh.pojo.User"> select * from mybatis1.user where id = #{id} </select> <!--insert 一个用户--> <insert id="addUser" parameterType="com.lhh.pojo.User"> insert into mybatis1.user(id,name,pwd) values(#{id},#{name},#{pwd}) </insert> <!--修改用户--> <update id="updateUser" parameterType="com.lhh.pojo.User"> update mybatis1.user set name= #{name},pwd =#{pwd} where id = #{id} </update> <!--删除一个用户--> <delete id="deleteUser" parameterType="int"> delete from mybatis1.user where id=#{id} </delete> </mapper>
-
测试
package com.lhh.dao; import com.lhh.pojo.User; import com.lhh.util.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserTest { @Test public void getUserList(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //第一中方法 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); //第二中方法 //List<User> userList = sqlSession.selectList("com.lhh.dao.UserMapper.getUserList"); for (User user : userList) { System.out.println(user); } //关闭资源 sqlSession.close(); } @Test public void getUserById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); //关闭资源 sqlSession.close(); } @Test public void addUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int res = mapper.addUser(new User(6, "lhh04", "123456")); System.out.println(res>0?"添加用户成功":"添加用户失败"); //提交事务 sqlSession.commit(); //关闭资源 sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(1,"呵呵","123456")); //提交事务 sqlSession.commit(); //关闭资源 sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession. getMapper(UserMapper.class); mapper.deleteUser(1); //提交事务 sqlSession.commit(); //关闭资源 sqlSession.close(); } }
3、Insert
<!--insert 一个用户-->
<insert id="addUser" parameterType="com.lhh.pojo.User">
insert into mybatis1.user(id,name,pwd) values(#{id},#{name},#{pwd})
</insert>
4、Update
<!--修改用户-->
<update id="updateUser" parameterType="com.lhh.pojo.User">
update mybatis1.user set name= #{name},pwd =#{pwd} where id = #{id}
</update>
5、Delete
<!--删除一个用户-->
<delete id="deleteUser" parameterType="int">
delete from mybatis1.user where id=#{id}
</delete>
注意点,增删改需要提交事务