七.MyBatis 简介
- Mybatis 开源免费框架.原名叫 iBatis,2010 在 google code,2013 年迁移到 github
- 作用: 数据访问层框架.
2.1 底层是对 JDBC 的封装. - mybatis 优点之一:
3.1 使用 mybatis 时不需要编写实现类,只需要写需要执行的 sql 命令
八. 环境搭建
- 导入 jar
其它包(单纯的介绍)
- 在 src 下新建全局配置文件(编写 JDBC 四个变量)
2.1 没有名称和地址要求
2.2 在全局配置文件中引入 DTD 或 schema
2.2.1 如果导入 dtd 后没有提示
Window–> preference --> XML --> XMl catalog --> add 按钮
2.3 全局配置文件内容
<?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>
<!-- default 引用 environment 的 id,当前所使用的环境 -->
<environments default="default">
<!-- 声明可以使用的环境 -->
<environment id="default">
<!-- 使用原生 JDBC 事务 -->
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/> <!-- 不能有空格,不然报Cannot find class: com.mysql.jdbc.Driver -->
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/> <!--不能有空格,不然报No suitable driver found for jdbc:mysql://localhost:3306/ssm -->
<property name="username" value="root"/> <!-- 不能有空格,不然报Access denied for user ' root'@'localhost' (using password: YES) -->
<property name="password" value="smallming"/> <!-- 不能有空格,不然报Access denied for user ' root'@'localhost' (using password: YES) -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bjsxt/mapper/FlowerMapper.xml"/>
</mappers>
</configuration>
环境搭建详解:
1.全局配置文件中内容
1.1 <transactionManager/> type 属性可取值
1.1.1 JDBC,事务管理使用 JDBC 原生事务管理方式
1.1.2 MANAGED 把事务管理转交给其他容器.原生 JDBC setAutoMapping(false);
1.2 <dataSouce/>type 属性
1.2.1 POOLED 使用数据库连接池
1.2.2 UNPOOLED 不实用数据库连接池,和直接使用 JDBC 一样
1.2.3 JNDI :java
- 新建以 mapper 结尾的包,在包下新建:实体类名+Mapper.xml
3.1 文件作用:编写需要执行的 SQL 命令
3.2 把 xml 文件理解成实现类.
3.3 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">
<!-- namesapce:理解成实现类的全路径(包名+类名) -->
<mapper namespace="a.b" >
<!-- id:方法名
parameterType:定义参数类型
resultType:返回值类型. 如果方法返回值是 list,在 resultType 中写 List 的泛型,
因为 mybatis
对 jdbc 封装,一行一行读取数据
-->
<select id="selAll"
resultType="com.bjsxt.pojo.Flower">
select * from flower
</select>
</mapper>
- 测试结果(只有在单独使用 mybatis 时使用,最后 ssm 整合时下面代
码不需要编写.)
InputStream is =Resources.getResourceAsStream("myabtis.xml");
//使用工厂设计模式
SqlSessionFactory factory = new
SqlSessionFactoryBuilder().build(is);
//生产 SqlSession
SqlSession session=factory.openSession();
List<Flower> list =
session.selectList("a.b.selAll");
for (Flower flower : list) {
System.out.println(flower.toString());
}
session.close();
十.数据库连接池
1.在内存中开辟一块空间,存放多个数据库连接对象.
2.JDBC Tomcat Pool,直接由 tomcat 产生数据库连接池.
3.图示
3.1 active 状态:当前连接对象被应用程序使用中
3.2 Idle 空闲状态:等待应用程序使用
4.使用数据库连接池的目的
4.1 在高频率访问数据库时,使用数据库连接池可以降低服务器系统压力,提升程序运行效率.
4.1.1 小型项目不适用数据库连接池
5.实现 JDBC tomcat Pool 的步骤.
5.1 在 web 项目的 META-INF 中存放 context.xml,在 context.xml 编写数据库连接池相关属性
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/ssm"
username="root"
password="smallming"
maxActive="50"
maxIdle="20"
name="test"
auth="Container"
maxWait="10000"
type="javax.sql.DataSource"
/>
</Context>
5.2 把项目发布到 tomcat 中,数据库连接池产生了
6.可以在 java 中使用 jndi 获取数据库连接池中对象
6.1 Context:上下文接口.context.xml 文件对象类型
6.2 代码:
Context cxt = new InitialContext();
DataSource ds = (DataSource)
cxt.lookup("java:comp/env/test");
Connection conn = ds.getConnection();
6.3 当关闭连接对象时,把连接对象归还给数据库连接池,把状态
改变成 Idle
十一. 三种查询方式
1.selectList() 返回值为 List<resultType 属性控制>
1.1 适用于查询结果都需要遍历的需求
List<Flower> list = session.selectList("a.b.selAll");
for (Flower flower : list) {
System.out.println(flower.toString());
}
2.selectOne() 返回值 Object, 2.1 适用于返回结果只是变量或一行数据时
int count = session.selectOne("a.b.selById");
Flower flower = session.selectOne("a.b.selById",4); //mapper通过parameterType设置参数类型、#{随意写}接收参数,大括号中的参数随意写;参数可为对象,为对象时#{对象的属性名}
3.selectMap() 返回值 Map
3.1 适用于需要在查询结果中通过某列的值取到这行数据的需求.
3.2 Map<key,resultType 控制>
Map<Object, Object> map = session.selectMap("a.b.c","column_name");
System.out.println(map);
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">
<!-- namesapce:理解成实现类的全路径(包名+类名) -->
<mapper namespace="a.b" >
<!-- id:方法名
parameterType:定义参数类型
resultType:返回值类型.
如果方法返回值是list,在resultType中写List的泛型,因为mybatis
对jdbc封装,一行一行读取数据
-->
<select id="selAll" resultType="com.bjsxt.pojo.Flower">
select id,name name123,price,production from flower
</select>
<!-- 查询数量
<select id="selById" resultType="int">
select count(*) from flower
</select>
-->
<select id="selById" parameterType="int" resultType="com.xmm.demo.pojo.Flower">
select * from test where id = #{suiyi}
</select>
<select id="c" resultType="com.bjsxt.pojo.Flower">
select id,name name123,price,production from flower
</select>
</mapper>