目录
第二步:将xml中的namespace属性换成Mapper接口全限名称
二、如何将xml中的参数与mapper层的参数对应(单个参数)
三、如何将xml中的参数与mapper层的参数对应(多个参数)
Blue的留声机
🎀🎀Hello,各位小伙伴们,好久不见,最近一直忙碌的去学东学西,就没啥时间写文章了,今天给各位带来的是xml版本去使用mybits,我们经常使用注解的方法去开发,但是这对于企业是远远不够的,所以学会使用xml配置文件的方式去使用mybits,显得尤为重要。所以俺,就带来了一篇自己的mybits笔记,相信这篇笔记以及黑马的视频,各位上手mybits,如鱼得水!!🎀🎀
视频推荐:
1、快速入手
第一步:导依赖
XML
<!-- mybits依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.14</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
</dependencies>
引入一点其他的依赖,测试用
XML
<!-- 添加slf4j日志api-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.6</version>
</dependency>
第二步:配置mybits-config.xml文件
根据mybits中文网入门教程:入门_MyBatis中文网
XML
<?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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/itheima"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载sql映射文件-->
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
第三步:编写sql映射文件BlogMapper.xml
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">
<mapper namespace="test">
<select id="selectBlog" resultType="Dao.Blog">
select * from blog
</select>
</mapper>
第四步:编写运行文件,执行sql
java
import Dao.Blog;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class mybitsdemo {
public static void main(String[] args) throws IOException {
// 1、加载mybits的核心配置文件,获取SqlsessionFactory
String resource = "mybits-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取sqlsession对象
SqlSession session =sqlSessionFactory.openSession();
//3、执行sql语句
List<Blog> Blog = session.selectList("test.selectBlog");//BlogMapper.xml中的namespace.下面的id
System.out.println(Blog);
//4、释放资源
session.close();
}
}
2、Mapper代理开发(企业中最常用)
mapper开发规则
概览:
第一步:定义与sql映射文件同名的Mapper接口
第二步:将xml中的namespace属性换成Mapper接口全限名称
第三步:编码
java
//mybits的代理开发
public class mybatsdemo2 {
public static void main(String[] args) throws IOException {
// 1、加载mybits的核心配置文件,获取SqlsessionFactory
String resource = "mybits-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取sqlsession对象
SqlSession session =sqlSessionFactory.openSession();
//3、执行sql语句
BlogMapper mapper = session.getMapper(BlogMapper.class);
List<Blog> blogs = mapper.selectBlog();
System.out.println(blogs);
//4、释放资源
session.close();
}
}
3、问题解决大全
一、如果实体类字段和数据库字段名不同,如何解决
利用resultMap
标签,id
自己取名字,type
对应类的名称,result
标签中column
对应数据库的字段名,property
对应java代码里面的类的字段名。然后在我们写代码的区域中在<select>
代码中加resultMap
后跟id名称。
当然还要id
标签,<id>
,这个是对主键的<result>
对应平常的字段名
二、如何将xml中的参数与mapper层的参数对应(单个参数)
单个参数:
利用占位符直接写
Mapper层:
java
public interface BlogMapper {
List<Blog> selectBlog(int id);
}
xml:
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">
<mapper namespace="Mapper.BlogMapper">
<select id="selectBlog" resultType="Dao.Blog">
select * from blog where id =#{1}
</select>
</mapper>
参数占位符:
特殊字符处理:
有时候需要>或者<或者其他特殊字符写在xml的sql语句中,但这样直接写,是不允许的,这样就提供了两种方式去写,我比较推荐第二种:CDATA区
将<号,写在CDATA区域中。
总结:
三、如何将xml中的参数与mapper层的参数对应(多个参数)
第一种:利用注解@Param
mapper层:
XML
public interface BlogMapper {
List<Blog> select(@Param("value1") int id,@Param("value2") String ws);
}
xml层:
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">
<mapper namespace="Mapper.BlogMapper">
<select id="select" resultType="Dao.Blog">
select * from blog where id=#{value1} and name =#{value2}
</select>
</mapper>
第二种:如果传递的参数来自同一个对象,直接传对象
启动层:
java
//mybits的代理开发
public class mybatsdemo2 {
public static void main(String[] args) throws IOException {
// 1、加载mybits的核心配置文件,获取SqlsessionFactory
String resource = "mybits-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取sqlsession对象
SqlSession session =sqlSessionFactory.openSession();
Blog blog =new Blog(1,"苏苏");
//3、执行sql语句
BlogMapper mapper = session.getMapper(BlogMapper.class);
List<Blog> blogs = mapper.select(blog);
System.out.println(blogs);
//4、释放资源
session.close();
}
}
Mapper层:
java
public interface BlogMapper {
List<Blog> select(Blog blog);
}
xml层:
java
<?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.BlogMapper">
<select id="select" resultType="Dao.Blog">
select * from blog where id=#{id} and name =#{name}
</select>
</mapper>
第三种:封装成map集合
启动层:
java
//mybits的代理开发
public class mybatsdemo2 {
public static void main(String[] args) throws IOException {
// 1、加载mybits的核心配置文件,获取SqlsessionFactory
String resource = "mybits-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取sqlsession对象
SqlSession session =sqlSessionFactory.openSession();
Map map =new HashMap();
map.put("id",1);
map.put("name","苏苏");
//3、执行sql语句
BlogMapper mapper = session.getMapper(BlogMapper.class);
List<Blog> blogs = mapper.select(map);
System.out.println(blogs);
//4、释放资源
session.close();
}
}
xml:
java
<?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.BlogMapper">
<select id="select" resultType="Dao.Blog">
select * from blog where id=#{id} and name =#{name}
</select>
</mapper>
mapper:
java
public interface BlogMapper {
List<Blog> select(Map map);
}
4、动态条件查询
if标签
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">
<mapper namespace="Mapper.BlogMapper">
<select id="select" resultType="Dao.Blog">
select * from blog
where
<if test="id !=null"> id=#{id} </if>
<if test="name!=null and name!= ''">and name=#{name}</if>
</select>
</mapper>
where标签
解决if标签里面会出现多余and的问题
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">
<mapper namespace="Mapper.BlogMapper">
<select id="select" resultType="Dao.Blog">
select * from blog
<where>
<if test="id !=null"> and id=#{id} </if>
<if test="name!=null and name!= ''">and name=#{name}</if>
</where>
</select>
</mapper>
choose标签:单条件动态查询
修改动态字段
结语:
🎀🎀🎀MyBatis作为一个成熟且强大的持久层框架,在Java开发领域有着广泛的应用。希望本教程能够成为您学习MyBatis的起点,激发您对数据库交互技术深入探索的兴趣。记住,实践是检验真理的唯一标准,只有不断动手实践,才能真正掌握MyBatis的精髓。期待您在未来的项目中,能够灵活运用MyBatis,构建出更加高效、健壮的数据持久层解决方案。加油,未来的开发者们!🎀🎀🎀