CRUD
目录
1. namespace
namespace中的包名要和Dao/Mapper接口的包名一致!
2. select
选择,查询语句
1. id
就是对应的namespace中的方法名
2. resultType
SQL语句执行的返回值
3. parameterType
参数类型(方法的参数)
其中,基本的参数类型直接写,而自定义的对象的类型需要写完整的包名以及类路径
只有一个基本参数类型的情况下,可以直接在sql中取到!
多个参数用Map或者注解!
3. CRUD
UserMapper接口
package com.wang.dao;
import com.wang.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);
}
UserMapper.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.wang.dao.UserMapper">
<!--select查询语句, id=方法的名字, resultType=返回的类型,写集合的泛型-->
<select id="getUserList" resultType="com.wang.pojo.User">
select * from mybatis.user;
</select>
<select id="getUserById" parameterType="int" resultType="com.wang.pojo.User">
select * from mybatis.user where id = #{id};
</select>
<!--insert,对象中的属性可以直接取出来-->
<insert id="addUser" parameterType="com.wang.pojo.User">
insert into mybatis.user (id, name, pwd) VALUES (#{id},#{name},#{pwd});
</insert>
<!--update-->
<update id="updateUser" parameterType="com.wang.pojo.User">
update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id};
</update>
<!--delete-->
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
</delete>
</mapper>
测试类
package com.wang.dao;
import com.wang.pojo.User;
import com.wang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void test(){
//第一步: 获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
//第一步: 获取sqlSession对象
sqlSession = MybatisUtils.getSqlSession();
//方式一: 执行SQL
//面向接口编程,向getMapper传递一个对应的接口的class,用反射
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//从得到的mapper对象中取出对应的方法,不用关心其实现
List<User> userList = userMapper.getUserList();
//方法二:
//List<User> userList = sqlSession.selectList("com.wang.dao.UserDao.getUserList");
for (User user : userList) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void testGetUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
//增删改必须要提交事务!
@Test
public void testAddUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(4, "王麻子", "123456789");
int num = mapper.addUser(user);
if (num > 0) {
System.out.println("插入成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestUpdateUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(4, "赵六", "987654");
int num = mapper.updateUser(user);
if (num > 0) {
System.out.println("修改成功!");
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void TestDeleteUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int num = mapper.deleteUser(4);
if (num > 0) {
System.out.println("删除成功!");
}
sqlSession.commit();
sqlSession.close();
}
}
4. 注意点
1. 增删改一定要提交事务
sqlSession.commit()
2. xml中的变量写法为#{}
{}中写pojo对应的变量名
3. 标签不要匹配错
4. 命名空间路径写.
5. resource绑定mapper需要使用路径,写/
6. 程序配置文件必须符合规范
7. maven资源没有导出的问题
由于Maven是约定大于配置,为了读取到不在resource目录下的资源,需要在Maven的pom.xml中加入如下的代码
父工程和子工程都要加!
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
5. 万能的Map
有时候,数据库的字段过多而要修改的字段很少的时候,使用Map ==> 传递一个Map对象给对应的方法,键值对为String,Object,分别为字段名和要修改的参数的值
UserMap接口
//万能的Map
int addUser2(Map<String,Object> map);
UserMap.xml
其中,字段名可以和数据库中的字段不一致,在使用时使用定义的字段名!
参数类型写map!
<!--使用map进行insert操作-->
<insert id="addUser2" parameterType="map">
insert into mybatis.user (id, name, pwd) VALUES (#{userId},#{userName},#{password});
</insert>
测试类
//增删改必须要提交事务!
@Test
public void testAddUser2() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("userId", 4);
map.put("userName", "王麻子");
map.put("password", "123456789");
int num = mapper.addUser2(map);
if (num > 0) {
System.out.println("插入成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
6. 模糊查询
UserMap接口
//模糊查询
List<User> getUserLike(String value);
UserMap.xml
<!--模糊查询-->
<select id="getUserLike" resultType="com.wang.pojo.User">
select * from mybatis.user where name like #{value}
</select>
测试类
Java代码运行的时候传递通配符%,这样比较安全
不要在sql中拼接%,非要拼接的话要写"%",这样可以防止sql注入
//模糊查询
@Test
public void testGetUserLike() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.getUserLike("%王%");
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}