目录
一、创建Mapper代理接口
二、配置Mapper代理映射文件
三、调用代理查询
四、代理包的另一种导入方式
五、mabatisX插件
六、实体类属性自定义映射
七、特殊字符
八、多值条件查询
九、动态参数加载
十、插入数据&&主键返回
一、创建Mapper代理接口
在java代码中创建代理接口
mapper.userMapper.java
package mapper;
import pojo.User;
import java.util.List;
public interface userMapper {
List selectall();
User selectid(int user_id);
}
二、配置Mapper代理映射文件
映射文件在资源目录里要和接口具有相同的全限定名称。
在资源目录中创建mapper文件夹,创建usermapper.xml文件
在这里面要注意,
mapper的namespace属性要填接口的全限定名,用于定位接口位置
id 则是接口的方法,用于确定被哪个方法调用
resultType="pojo.User" 这个·则是接收返回值的对象。
在java代码中创建代理接口
mapper.userMapper.java
映射文件在资源目录里要和接口具有相同的全限定名称。
在资源目录中创建mapper文件夹,创建usermapper.xml文件
在这里面要注意,
mapper的namespace属性要填接口的全限定名,用于定位接口位置
id 则是接口的方法,用于确定被哪个方法调用
resultType="pojo.User" 这个·则是接收返回值的对象。
三、调用代理查询
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;
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 users = sqlSession.selectList("test.selectall");
//3.1 获取代理接口对象
userMapper usermapper =sqlSession.getMapper(userMapper.class);
List 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代理开发提供了包导入的方式简化映射
执行结果:
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代理开发提供了包导入的方式简化映射
数据库信息参考:https://blog.csdn.net/qq_45514735/article/details/125702127?spm=1001.2014.3001.5501
五、mabatisX插件
使用这个插件后映射文件和接口类之间会建立关联,通过点击
图标可以实现快速切换,并且会自动补全生成映射或方法。
六、实体类属性自定义映射
在实际工作过程中,实体类与数据库条目会出现不一致的情况,这样的话就没办法正常获取数据,我们需要修改配置来对每一个条目进行映射。
一、数据库查询条目起别名
二、sql片段
三、resultMap自定义映射
一、通过as在数据库查询中起别名,这是数据库相关知识,默认大家都会,就不细说了
二、sql片段:
* select from sjj where id= #{id};
三、resultMap
select * from sjj;
七、特殊字符
1、字符转义
2、CDATA区间
八、多值条件查询
1、字符转义
2、CDATA区间
select from sjj where id= #{id} and 名称 = #(名称);
一、@Param注解占位符
leibie selectid(@Param("id")int id,@Param("名称")String 名称);二、实体类对象参数
leibie selectAllByIdLeibie(leibie leibie);selectfrom 类别 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 // Listusers = 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 from 类别 where id= #{id} and 名称 = #{名称}; @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 // Listusers = 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 from 类别 where id= #{id} and 名称 = #{名称}; 解决逻辑符号问题,用where标签替换where语法
select from 类别 id= #{id} and 名称 = #{名称}; 2.单条件的动态查询
choose(when,otherwise) 类似于java的swith语句,when类似于case,otherwise类似于default
select from 类别 id= #{id} 名称 = #{名称}; 1=1;
十、插入数据&&主键返回
void add(leibie leibie);
insert into 类别 (名称)
values (#{名称});
@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 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是正确的,主键返回生效了
我们并没有为对象设置id值,但是最后的查询结果中id是正确的,主键返回生效了



