❤️使用Mybatis对数据库进行增删改查!!!
❤️namespace的用法
namespace中的包名要和接口一致;
❤️select用法
- id:就是对应的namespace的方法名
- resultType:sql语句的返回值!
- parameterType: 参数类型!
- 编写接口
- 编写对应的mapper中的对应语句
- 测试
❤️1.编写接口
package com.kk.dao;
import com.kk.pojo.User;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//模糊查询
List<User> getUserLike(String value);
//查询全部用户
List<User> getUserList();
//根据ID查询用户
User getUserById(int id);
User getUserById2(HashMap<String, Object> map);
//insert一个用户
int addUser(User user);
//万能的Map
int addUser2(Map<String,Object> map);
//修改用户
int updateUser(User user);
//删除一个用户
int deleteUser(int id);
}
❤️2.编写Mapper.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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.kk.dao.UserMapper">
<!-- 模糊查询-->
<select id="getUserLike" resultType="com.kk.pojo.User">
select * from mybatis.user where name like #{value}
</select>
<!-- select查询语句-->
<select id="getUserList" resultType="com.kk.pojo.User">
select * from mybatis.user
</select>
<!-- select查询语句-->
<select id="getUserById" parameterType="int" resultType="com.kk.pojo.User" >
select * from mybatis.user where id = #{id};
</select>
<select id="getUserById2" parameterType="map" resultType="com.kk.pojo.User" >
select * from mybatis.user where id = #{helloId} and name=#{name};
</select>
<!-- 插入语句-->
<!-- 对象中的属性可以直接取出来-->
<insert id="addUser" parameterType="com.kk.pojo.User">
insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd});
</insert>
<!-- 传递map的key-->
<!-- <insert id="addUser2" parameterType="map">-->
<!-- insert into mybatis.user(id,name,pwd) values(#{userid},#{userName},#{passWord});-->
<!-- </insert>-->
<!-- 修改用户-->
<update id="updateUser" parameterType="com.kk.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id};
</update>
<!-- 删除一个用户-->
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
</mapper>
❤️3.测试类
package com.kk.dao;
import com.kk.pojo.User;
import com.kk.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.List;
public class userDaoTest {
//模糊查询
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("%王%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//方式一:执行SQL-->getMapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sql
sqlSession.close();
}
}
@Test
public void getUserId(){
SqlSession sqlSession = MybatisUtil.getSqlSession();//通过MybatisUtil获得Sql Seesion
UserMapper mapper = sqlSession.getMapper(UserMapper.class);//通过这个获得UserMapper这个接口
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void getUserId2(){
SqlSession sqlSession = MybatisUtil.getSqlSession();//通过MybatisUtil获得Sql Seesion
UserMapper mapper = sqlSession.getMapper(UserMapper.class);//通过这个获得UserMapper这个接口
HashMap<String, Object> map = new HashMap<>();
map.put("helloId",1);
User user = mapper.getUserById2(map);
System.out.println(user);
sqlSession.close();
}
//增删改需要提交事务
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(4, "大白", "123456"));
if (res>0){
System.out.println("插入成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
//增删改需要提交事务 Map
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("userid",6);
map.put("userName","程峰");
map.put("passWord","666666");
mapper.addUser2(map);
//提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(5,"晓晓","123789"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
// //方式二:
// List<User> userList = sqlSession.selectList("com.kk.UserDao.getUserList");
注意点:增删改需要提交事务。
## Map用法
假如我们的实体类属性过多,用map,传递map的key
接口
User getUserById2(HashMap<String, Object> map);
Mapper.xml
<select id="getUserById2" parameterType="map" resultType="com.kk.pojo.User" >
select * from mybatis.user where id = #{helloId} and name=#{name};
</select>
测试
@Test
public void getUserId2(){
SqlSession sqlSession = MybatisUtil.getSqlSession();//通过MybatisUtil获得Sql Seesion
UserMapper mapper = sqlSession.getMapper(UserMapper.class);//通过这个获得UserMapper这个接口
HashMap<String, Object> map = new HashMap<>();
map.put("helloId",1);
User user = mapper.getUserById2(map);
System.out.println(user);
sqlSession.close();
}
❤️4.模糊查询
java代码执行的时候,传递通配符%
接口
//模糊查询
List<User> getUserLike(String value);
Mapper.xml
<!-- 模糊查询-->
<select id="getUserLike" resultType="com.kk.pojo.User">
select * from mybatis.user where name like #{value}
</select>
测试
//模糊查询
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("%王%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}