MyBatis基础及查询
一.项目中命名规范
层 | 常用名 | 作用/最常用名 | |
---|---|---|---|
1. | 项目名: | 没有要求,不要中文 | |
2. | 包: | com.liwei | 公司域名倒写 |
3. | 数据访问层 | dao,persist | mapper |
4. | 实体 | entity,model,bean,javabean | pojo |
5. | 业务逻辑 | biz | service |
6. | 控制器 | controller,action,web | servlet |
7. | 过滤器 | filter | |
8. | 异常 | exception | |
9. | 监听器 | listener | |
10. | 注释 | ||
10.1 | 注释 | /** */ | 类上和方法上使用文档注释 |
10.2 | 注释 | /* */ | 在方法里面使用 |
11 | 类 | 大驼峰 | |
12 | 方法,属性 | 小驼峰 |
二.框架是什么?
- 框架:软件的半成品.未解决问题制定的一套约束,在提供功能基础
上进行扩充. - 框架中一些不能被封装的代码(变量),需要使用框架者新建一个
xml 文件,在文件中添加变量内容.
2.1 需要建立特定位置和特定名称的配置文件.
2.2 需要使用 xml 解析技术和反射技术. - 常用概念
3.1 类库:提供的类没有封装一定逻辑.
举例:类库就是名言警句,写作文时引入名言警句
3.2 框架:区别与类库,里面有一些约束.
举例:框架是填空题
三.Mybatis简介
- Mybatis 开源免费框架.原名叫 iBatis,2010 在 google code,2013 年迁移到 github
- 作用: 数据访问层框架.
2.1 底层是对 JDBC 的封装. - mybatis 优点之一:
3.1 使用 mybatis 时不需要编写实现类,只需要写需要执行的 sql 命令
四.环境搭建基本
4.1 导入jar
4.2 在src下新建全局配置文件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>
<!--default引用environments的id,当前所使用的的环境-->
<environments default="default">
<!--声明可以使用的环境-->
<environment id="default">
<!--使用原生JDBC-->
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--加载sql文件-->
<mappers>
<mapper resource="com/wenli/mapper/FlowerMapper.xml"></mapper>
</mappers>
</configuration>
4.3 新建以mapper结尾的包,在包下新建:实体类名+Mapper.xml
- 文件作用:编写需要执行的SQL命令
- 被xml文件理解为类
- 注意:当查询时,有表列名与实体类不统一时,可以采用起别名方式
<select id="selAll" resultType="com.wenli.pojo.Flower">
-- 实体类 与数据库 表可以不同,查询时起别名将其统一 表列名---属性名
select id,name name123,price,production from flower
</select>
- 而且,反射赋值在从mybatis3.2后直接通过属性赋值,之前从setter赋值
- 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.wenli.pojo.Flower">
-- 直接通过属性赋值(从3.2后,之前从setter赋值),实体类 与数据库 表可以不同,查询时起别名将其统一 表列名---属性名
-- select id,name name123,price,production from flower
select * from flower
</select>
<select id="selById" resultType="int">
select count(*) from flower;
-- select count(*) from flower where id = 1;
</select>
<select id="c" resultType="com.wenli.pojo.Flower">
select * from flower;
</select>
</mapper>
4.4 简单测试(只有单独使用mybatis时使用,最后ssm整合时下面的代码不需要写),直接运行test01.java
public class test01 {
public static void main(String[] args) throws IOException {
// 给路径,读取
InputStream is = Resources.getResourceAsStream("mybatis.xml");
// 工厂设计模式
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
// 生产SqlSession
SqlSession session = factory.openSession();
// 调用方法,
// 1. 返回集合
List<Flower> list = session.selectList("a.b.selAll");
// 打印
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 关闭流
session.close();
}
}
五.环境搭建详解(全局配置文件中的内容)
5.1 <transactionManager type=""></transactionManager>
<transactionManager type=""></transactionManager>
中type属性可取值
1.1 JDBC :事务管理使用JDBC原生事务管理方式
1.2 MANAGED:把事务管理转交给其他容器.原生 JDBC 事务加setAutoMapping(false);
1.3
5.2 <dataSouce/>type
属性
- POOLED 使用数据库连接池
- UNPOOLED 不实用数据库连接池,和直接使用 JDBC 一样
- JNDI :java 命名目录接口技术
六.三种查询方式
返回值类型是靠别名的,例如返回Integer类型,用int
6.1 selectList()返回值为 List<resultType 属性控制>
<select id="selAll" resultType="com.wenli.pojo.Flower">
select * from flower
</select>
适用于查询结果都需要遍历的需求
// 1. 返回集合
List<Flower> list = session.selectList("a.b.selAll");
// 打印
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
6.2 selectOne() 返回值 Object
<select id="selById" resultType="int">
select count(*) from flower;
</select>
适用于返回结果只是变量或一行数据时
// 2. 返回数字
int o = session.selectOne("a.b.selById");
System.out.println(o);
6.3 selectMap() 返回值 Map
<select id="c" resultType="com.wenli.pojo.Flower">
select * from flower;
</select>
- 适用于需要在查询结果中通过某列的值取到这行数据的需求
- =Map<key,resultType 控制>
// 3. 把数据库的某个列的值当做 map的key-------用来去重,没啥用
// 做个通讯录,
Map<Object, Object> map = session.selectMap("a.b.c", "name");
Set<Map.Entry<Object, Object>> entries = map.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
System.out.println(entry);
}
6.4 结果
Flower{id=1, name='矮牵牛', price=2.5, production='南美阿根廷'}
Flower{id=2, name='白日星', price=5.0, production='墨西哥'}
Flower{id=3, name='半枝莲', price=4.3, production='巴西'}
Flower{id=4, name='ss', price=10.0, production='诶IE飞机地方'}
Flower{id=5, name='凄凄切切群', price=100.0, production='二维翁翁·'}
Flower{id=6, name='凄凄切切群', price=100.0, production='二维翁翁·'}
Flower{id=7, name='凄凄切切群', price=100.0, production='二维翁翁·'}
Flower{id=8, name='ss吾问无为谓', price=10.0, production='诶IE飞机地方'}
Flower{id=9, name='喂喂喂翁', price=10.0, production='2222'}
Flower{id=10, name='喂喂喂翁', price=10.0, production='2222'}
Flower{id=11, name='11', price=11.0, production='111'}
Flower{id=12, name='11', price=11.0, production='111'}
Flower{id=13, name='77', price=77.0, production='77'}
Flower{id=14, name='ss', price=10.0, production='诶IE飞机地方'}
Flower{id=15, name='ss', price=10.0, production='诶IE飞机地方'}
Flower{id=16, name='ss', price=10.0, production='诶IE飞机地方'}
Flower{id=17, name='ss', price=10.0, production='诶IE飞机地方'}
Flower{id=18, name='凄凄切切群', price=10.0, production='诶IE飞机地方'}
Flower{id=19, name='凄凄切切群', price=10.0, production='诶IE飞机地方'}
Flower{id=20, name='1', price=1.0, production='1'}
Flower{id=21, name='2', price=2.0, production='2'}
Flower{id=22, name='2', price=2.0, production='2'}
Flower{id=23, name='2', price=2.0, production='2'}
Flower{id=24, name='3', price=3.0, production='3'}
Flower{id=25, name='ss', price=10.0, production=''}
Flower{id=26, name='ss', price=10.0, production=''}
26
ss=Flower{id=26, name='ss', price=10.0, production=''}
ss吾问无为谓=Flower{id=8, name='ss吾问无为谓', price=10.0, production='诶IE飞机地方'}
11=Flower{id=12, name='11', price=11.0, production='111'}
77=Flower{id=13, name='77', price=77.0, production='77'}
1=Flower{id=20, name='1', price=1.0, production='1'}
2=Flower{id=23, name='2', price=2.0, production='2'}
3=Flower{id=24, name='3', price=3.0, production='3'}
矮牵牛=Flower{id=1, name='矮牵牛', price=2.5, production='南美阿根廷'}
喂喂喂翁=Flower{id=10, name='喂喂喂翁', price=10.0, production='2222'}
凄凄切切群=Flower{id=19, name='凄凄切切群', price=10.0, production='诶IE飞机地方'}
半枝莲=Flower{id=3, name='半枝莲', price=4.3, production='巴西'}
白日星=Flower{id=2, name='白日星', price=5.0, production='墨西哥'}
6.5 实体类Flower.java
package com.wenli.pojo;
public class Flower {
private int id;
private String name;
private double price;
private String production;
public Flower() {
}
public Flower(int id, String name, double price, String production) {
this.id = id;
this.name = name;
this.price = price;
this.production = production;
}
public Flower(String name, double price, String production) {
this.name = name;
this.price = price;
this.production = production;
}
@Override
public String toString() {
return "Flower{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", production='" + production + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
}