Mybatis 基础系列一
创建第一个 mybatis 程序
需要配置项
1, 在 conf.xml 的需要配置配置两个标签数据库连接和 mapper,xml 文件加载信息
<-- 进行数据库环境参数的配置
default 里面是需要采用的数据库环境, 因为可能有 development,test 等多种环境
- -->
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <datasource type="POOLED">
- <property name="driver", value=""/>
- <property name="url", value=""/>
- <property name="user" value=""/>
- <property name="password" value=""/>
- </datasource>
- </environment>
- <-- 配置 mapper.xml 映射文件 -->
- <mappers>
- <mapper resource=""></mapper>
- </mappers>
- </environments>
2, 编写 mapper.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="org.lanqiao.entity.personMapper">
- <select id="queryPersonById" resultType="org.lanqiao.entity.Person" parameterType="int">
- select * from person where id = #{id}
- </select>
- </mapper>
mybatis 规定一个 SQL 语句最多只能有一个输入参数
3, 编写与数据库中对应的类
4, 编写测试类 (可选)
- public static void main(String[] args) throws IOException {
- // 加载 MyBatis 配置文件 (为了访问数据库)
- Reader reader = Resources.getResourceAsReader("conf.xml") ;
- SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader) ;
- //session - connection
- SqlSession session = sessionFactory.openSession() ;
- //statement 用来定位 SQL 语句, 是 XXXmapper.xml 中的 namespace.id 构成的
- String statement = "org.lanqiao.entity.personMapper.queryPersonById" ;
- // 有参数时, 将参数作为第二个参数跟在 statement 后面, 若没有则调用只有一个参数的 select 函数
- Student person = session.selectOne( statement,1 ) ;
- System.out.println(person);
- session.close();
- }
mybatis 程序升级版
每次写一个定位 SQL 的 statement 很麻烦, 我们可以采用动态代理或者接口开发的方式
与第一种方式的不同之处
采用约定优于配置的思想,, 省略掉 statement, 根据约定直接定位出 SQL 语句
步骤
conf.xml,XXXmapper.xml 以及实体类的编写都与上述方法相同,
不同的是增加一个 XXXmapper.java 的接口,
要求:
1, mapper.java 类名应该与 mapper.xml 里面的 namespace 相同
2, mapper.java 中的方法名称与 mapper.xml 里面对应的每一个 SQL 语句对应的 id 相同
3,mapper.java 中方法的输入参数类型与 mapper.xml 中的 @parameterType 相同 (若没有 @parameterType 则为无参函数)
4,mapper.java 中方法的返回值类型与 mapper.xml 中的 @resultType 一致
则测试函数中可以修改为
StudentMapper studentMapper = session.getMapper(StudentMapper.class) ;
studentMapper. 方法 ();
程序优化
优化一: 将数据库连接信息单独放在一个配置文件 db.properties
为了方便查看与修改有关数据库连接的信息, 我们可以选择将其单独放入一个. properties 的配置文件中, 采用键值对方式表示
然后在 conf.xml 文件里面引入这个. properties 文件之后用 ${} 的形式引用当中的变量与值
示例:
db.properties 文件:
- driver=oracle.jdbc.OracleDriver
- url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
- username=scott
- password=tiger
conf.xml 文件:
- <properties resource="db.properties"/>
- <dataSource type="POOLED">
- <property name="driver" value="${driver}"/>
- <property name="url" value="${url}"/>
- <property name="username" value="${username}"/>
- <property name="password" value="${password}"/>
- <dataSource/>
优化二: 设置别名
在 mapper.xml 文件中 @resultType 如果是对象类型的话要写完整
例如 resultType="org.lanqiao.entity.Person"
通过设置别名我们可以简略这种写法
设置别名的方式
conf.xml 文件中:
- <typeAliases>
- <-- 单个设置别名 -->
- <typeAlias type="org.lanqiao.entity.Person" alias="Person">
- <-- 批量设置别名 则这个包下面所有类的别名都是去除包名之后的类名 -->
- <package name="org.lanqiao.entity">
- <typeAliases>
来源: https://www.cnblogs.com/huanglf714/p/10767170.html