目录
一、创建Mapper代理接口
在java代码中创建代理接口
mapper.userMapper.java
package mapper;
import pojo.User;
import java.util.List;
public interface userMapper {
List<User> selectall();
User selectid(int user_id);
}
二、配置Mapper代理映射文件
映射文件在资源目录里要和接口具有相同的全限定名称。
在资源目录中创建mapper文件夹,创建usermapper.xml文件
在这里面要注意,
mapper的namespace属性要填接口的全限定名,用于定位接口位置
id 则是接口的方法,用于确定被哪个方法调用
resultType="pojo.User" 这个·则是接收返回值的对象。
<?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="mapper.userMapper">
<!-- 此处填接口-->
<select id="selectall" resultType="pojo.User">
<!-- 此处田接口方法和返回类型,下面填具体语句-->
select * from user;
</select>
<select id="selectid" resultType="pojo.User">
<!-- 此处田接口方法和返回类型,下面填具体语句-->
select * from user where user_id= #{user_id};
</select>
</mapper>
三、调用代理查询
import mapper.userMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/*mapper代理开发*/
public class demo2 {
public static void main(String[] args) throws IOException {
//1.加载核心配置,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取sqlsiooin
SqlSession sqlSession=sqlSessionFactory.openSession();
//3.执行sql
// List<User> users = sqlSession.selectList("test.selectall");
//3.1 获取代理接口对象
userMapper usermapper =sqlSession.getMapper(userMapper.class);
List<User> users = usermapper.selectall();
User selectid = usermapper.selectid(3);
System.out.println(selectid.toString());
System.out.println(users);
//4.关闭资源
sqlSession.close();
}
}
执行结果:
User{user_id=3, name='孟凡杰', psw='孟凡杰', id_m='5f083a11a4666', tim_id=20200710, gm=1}
[User{user_id=1, name='112', psw='1', id_m='6291fc72de5e6', tim_id=20220528, gm=1}, User{user_id=2, name='1212', psw='1212', id_m='62a1780eccc87', tim_id=20220609, gm=1}, User{user_id=3, name='孟凡杰', psw='孟凡杰', id_m='5f083a11a4666', tim_id=20200710, gm=1}, User{user_id=4, name='4', psw='4', id_m='5f083a310e383', tim_id=20200710, gm=null}]进程已结束,退出代码0
四、代理包的另一种导入方式
除了直接
<mapper resource="mapper/userMapper.xml"/>外,mapper代理开发提供了包导入的方式简化映射
<package name="mapper"/>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://alpine:3306/class"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="mapper/userMapper.xml"/>-->
<!-- 新方法-->
<package name="mapper"/>
</mappers>
</configuration>
数据库信息参考:https://blog.csdn.net/qq_45514735/article/details/125702127?spm=1001.2014.3001.5501
五、mabatisX插件
使用这个插件后映射文件和接口类之间会建立关联,通过点击
图标可以实现快速切换,并且会自动补全生成映射或方法。
六、实体类属性自定义映射
在实际工作过程中,实体类与数据库条目会出现不一致的情况,这样的话就没办法正常获取数据,我们需要修改配置来对每一个条目进行映射。
一、数据库查询条目起别名
二、sql片段
三、resultMap自定义映射
一、通过as在数据库查询中起别名,这是数据库相关知识,默认大家都会,就不细说了
二、sql片段:
<sql id="select_id">*</sql> <!-- sql片段--> <select id="selectid" resultType="pojo.sjj"> <!-- 此处田接口方法和返回类型,下面填具体语句--> select <include refid="select_id"></include> <!--导入sql片段--> from sjj where id= #{id}; </select>
三、resultMap
<resultMap id="sjjMapper" type="pojo.sjj"> <result column="id" property="id"></result> <!-- 映射,将实体类中的属性与数据表中的属性做映射,适用于命名不一致的情况下,column是数据库中的条目property是实体类中的属性。,--> </resultMap> <!-- 此处填接口--> <select id="selectall" resultMap="sjjMapper"> <!-- 此处田接口方法和返回类型,下面填具体语句,这里的实体类换成了resultMap--> select * from sjj; </select>
七、特殊字符
1、字符转义
2、CDATA区间
<![CDATA[
其中的内容会被当成纯文本
]]>
八、多值条件查询
<select id="selectid" resultType="pojo.leibie">
<!-- 此处田接口方法和返回类型,下面填具体语句-->
select
<include refid="select_id"></include>
<!--导入sql片段-->
from sjj where id= #{id} and 名称 = #(名称);
</select>
一、@Param注解占位符
leibie selectid(@Param("id")int id,@Param("名称")String 名称);
二、实体类对象参数
leibie selectAllByIdLeibie(leibie leibie);
<!-- 此处田接口方法和返回类型,下面填具体语句--> select <include refid="select_id"></include> <!--导入sql片段--> from 类别 where id= #{id} and 名称 = #{名称};
@Test public void testduiciangcanshu1() throws IOException { int id=5; String 名称="其他"; //1.加载核心配置,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlsiooin SqlSession sqlSession=sqlSessionFactory.openSession(); //3.执行sql // List<User> users = sqlSession.selectList("test.selectall"); //3.1 获取代理接口对象 //执行对应接口方法 //列出结果 leibieMapper leibiemapper = sqlSession.getMapper(leibieMapper.class); leibie leibie=new leibie(); leibie.setId(id); leibie.set名称(名称); leibie selectid = leibiemapper.selectAllByIdLeibie(leibie); System.out.println(selectid.toString()); //4.关闭资源 sqlSession.close(); }
运行结果:ok
三、Map集合
leibie selectAllByIdMap(Map map);
<select id="selectAllByIdMap" resultType="pojo.leibie"> <!-- 此处田接口方法和返回类型,下面填具体语句--> select <include refid="select_id"></include> <!--导入sql片段--> from 类别 where id= #{id} and 名称 = #{名称}; </select>
@Test public void testMap() throws IOException { int id=5; String 名称="其他"; //1.加载核心配置,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlsiooin SqlSession sqlSession=sqlSessionFactory.openSession(); //3.执行sql // List<User> users = sqlSession.selectList("test.selectall"); //3.1 获取代理接口对象 //执行对应接口方法 //列出结果 leibieMapper leibiemapper = sqlSession.getMapper(leibieMapper.class); Map map=new HashMap(); map.put("id",5); map.put("名称",名称); leibie selectid = leibiemapper.selectAllByIdMap(map); System.out.println(selectid.toString()); //4.关闭资源 sqlSession.close(); }
九、动态参数加载
1、多条件的动态查询
if 动态条件查询,
if:判断条件
test逻辑表达式
<select id="selectAllByIdMap" resultType="pojo.leibie"> <!-- 此处田接口方法和返回类型,下面填具体语句--> select <include refid="select_id"></include> <!--导入sql片段--> from 类别 where <if test="id!=null"> id= #{id} </if> <if test="名称!=null and 名称!=''"> and 名称 = #{名称};</if> </select>
解决逻辑符号问题,用where标签替换where语法
<select id="selectAllByIdMap" resultType="pojo.leibie"> <!-- 此处田接口方法和返回类型,下面填具体语句--> select <include refid="select_id"></include> <!--导入sql片段--> from 类别 <where> <if test="id!=null"> id= #{id} </if> <if test="名称!=null and 名称!=''"> and 名称 = #{名称}; </if> </where> </select>
2.单条件的动态查询
choose(when,otherwise) 类似于java的swith语句,when类似于case,otherwise类似于default
<select id="selectAllByIdLeibie" resultType="pojo.leibie"> <!-- 此处田接口方法和返回类型,下面填具体语句--> select <include refid="select_id"></include> <!--导入sql片段--> from 类别 <where > <choose> <when test="id!=null"> id= #{id} </when> <when test="名称!=null and 名称!=''"> 名称 = #{名称}; </when> <otherwise> 1=1; </otherwise> </choose> </where> </select>
十、插入数据&&主键返回
void add(leibie leibie);
<insert id="add" useGeneratedKeys="true" keyProperty="id">
<!--useGeneratedKeys="true"开启获取返回值 keyProperty="id"设定返回值列名-->
insert into 类别 (名称)
values (#{名称});
</insert>
@Test
public void testAdd2() throws IOException {
String 名称="克苏鲁";
leibie leibie = new leibie();
leibie.set名称(名称);
//1.加载核心配置,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取sqlsiooin
SqlSession sqlSession=sqlSessionFactory.openSession();
// SqlSession sqlSession=sqlSessionFactory.openSession(true);//自动提交事务
//3.执行sql
// List<User> users = sqlSession.selectList("test.selectall");
//3.1 获取代理接口对象
//执行对应接口方法
leibieMapper mapper = sqlSession.getMapper(leibieMapper.class);
mapper.add(leibie);
//提交事务,默认不提交,需要手动提交
sqlSession.commit();
mapper.selectAllByIdLeibie(leibie);
System.out.println(leibie.getId());
//4.关闭资源
sqlSession.close();
}
我们并没有为对象设置id值,但是最后的查询结果中id是正确的,主键返回生效了
十一、动态修改数据
十二、批量删除
动态sql遍历
十三、mybatis参数封装