idea中maven资源过滤问题一定要配置,否则会出问题的
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
mybatis接口绑定及多参传递
接口绑定
在配置文件中使用进行扫描接口和mapper.xml
<mappers>
<package name="com.tqq.mapper"/>
</mappers>
在mapper下建立接口UserMapper
List<User> selAll();
在mapper建立一个UserMapper.xml文件
注意:此时namespace中的值是接口的全路径(包名+接口名)
增删改查中的id值为接口中的方法名
<?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.tqq.mapper.UserMapper">
<select id="selAll" resultType="user">
select * from user
</select>
</mapper>
测试
@org.junit.Test
public void test2() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
for (User user : userMapper.selAll()) {
System.out.println(user);
}
}
多参传递
接口中的方法有两种写法
第一种
User selByNameByPwd( String name,String pwd);
第二种
User selByNameByPwd(@Param("name") String name, @Param("pwd") String pwd);
分别对应两种不同的xml文件中的参数传递方式
第一种
<select id="selByNameByPwd" resultType="user">
select * from user where name = #{0} and pwd = #{0}
</select>
或者
<select id="selByNameByPwd" resultType="user">
select * from user where name = #{param1} and pwd = #{param2}
</select>
第二种
Usermapper.xml文件中要是下面方法来传递参数
<select id="selByNameByPwd" resultType="user">
select * from user where name = #{name} and pwd = #{pwd}
</select>
多参传递使用Map
int addUser2(Map<String,Object> map);
<!-- 对象中的属性可以直接取出来,传递map的key-->
<select id="selByNamePwd" parameterType="map" resultType="user">
select * from user where name = #{name} and pwd = #{pwd};
</select>
@org.junit.Test
public void test4() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("name","张三");
map.put("pwd","456");
User user = userMapper.selByNamePwd(map);
System.out.println(user);
}
接口方式的增删改
书写的位置和上面的一致
对象中的属性,可以直接取出来
增
int insertUser(User user);
<insert id="insertUser" parameterType="com.tqq.pojo.User">
insert into user (id, name, pwd) values (default ,#{name},#{pwd});
</insert>
删
int delById(int id);
<delete id="delById" parameterType="int">
delete from user where id = #{id};
</delete>
改
int updUser(User user);
<update id="updUser" parameterType="com.tqq.pojo.User">
update user set name= #{name},pwd = #{pwd} where id = #{id} ;
</update>
注:增删改需要提交事务
session.commit();
模糊查询
1、Java代码执行的时候传递通配符% %
接口:
List<User> selByName2(String name);
*.xml文件中的sql语句
<select id="selByName2" resultType="user">
select * from user where name like #{name};
</select>
执行时:
List<User> list= userMapper.selByName2("%山%");
2、在sql拼接时,使用通配符
接口:
List<User> selByName1(String name);
*.xml文件中的sql:
<select id="selByName1" resultType="user">
select * from user where name like "%"#{name}"%";
</select>
执行时:
List<User> list= userMapper.selByName1("山");
参考
公众号狂神说
尚学堂教学视频