一、Mybatis简介:
1. 简介:
Mybatis开源免费框架,原名叫iBatis,2010在google code,2013年迁移到github上.
2.作用:
数据访问层框架。
2.1底层就是对JDBC的封装。
3.Mybatis优点:
3.1使用Mybatis时不需要编写实现类,只需要写需要执行的sql命令
二、环境搭建:
1.导jar包
2.配置全局配置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>
<!-- 配置设置项-->
<settings>
<!--配置日志方式>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--配置包别名-->
<typeAliases>
<package name="com.test.pojo"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<!--配置事务方式这种JDBC配置是jdbc自动管理,也可以交给Spring管理 MANAGED-->
<transactionManager type="JDBC" />
<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>
<mappers>
<!-- 这里可以配置资源文件、包、或者类都行 -->
<mapper resource="com/test/mapper/StudentMapper.xml"/>
<mapper resource="com/test/mapper/TeacherMapper.xml"/>
</mappers>
</configuration>
3.对应的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="com.test.mapper.FlowerMapper">
<select id="selAll" resultType="com.test.pojo.Flower">
select * from flower
</select>
<select id="selMap" resultType="com.test.pojo.Flower">
select * from flower
</select>
</mapper>
4.对应的测试类代码:
/**
* @author QuLei
*
*/
public class Test {
public static void main(String[] args) throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = (new SqlSessionFactoryBuilder()).build(is);
SqlSession session = factory.openSession();
/* List<Flower> list = session.selectList("com.test.mapper.FlowerMapper.selAll");
for (Flower flower : list) {
System.out.println(flower);
}*/
//第一个参数为要执行的sql语句,第二个参数为要将那个字段的值作为map的key
Map<String, Flower> map = session.selectMap("com.test.mapper.FlowerMapper.selMap", "name");
Set<Entry<String, Flower>> entry = map.entrySet();
for (Entry<String, Flower> entry2 : entry) {
Flower flower = entry2.getValue();
String key = entry2.getKey();
System.out.println(key);
}
session.close();
}
}
5.使用session直接调用API操作
使用session直接调用API操作时方法只有两个参数可以传,第一个字符串表示要执行的sql的字符串,第二各参数为传过去的参数。其中参数如果为基本类型或String类型时后面去参数时只能用#{0},如果java代码中传过去的参数为对象时用#{对象的属性名}取参数。也可以传map过去取的时候拿#{key}来取。如下代码:
/**
* @author QuLei
*测试增删改查等参数传递
*/
public class Insert {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = (new SqlSessionFactoryBuilder()).build(is);
//默认开启的session的事务处理是关闭的,需要进行手动事务提交
SqlSession session = factory.openSession();
People p = new People();
p.setName("测试");
p.setAge(20);
int index = session.insert("com.test.mapper.PeopleMapper.inst", p);
session.commit();
session.close();
System.out.println(index);
System.out.println(p);
}
}
/**
* @author QuLei
*测试mybatis框架的日志
*另外测试参数的传递 查询时只有三种 selectList selectOne selectMap 需要传参数
*/
public class Test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = (new SqlSessionFactoryBuilder()).build(is);
SqlSession session = factory.openSession();
/*
* 测试分页的代码其中使用map形式向xml传递参数
* */
int pageSize = 3; //每页的数
int pageNum = 5; //第几页
int startNum = (pageNum - 1)*pageSize ; //起始行
Map<String,Integer> map = new HashMap<>();
map.put("startNum", startNum);
map.put("pageSize", pageSize);
List<People> list = session.selectList("com.test.mapper.PeopleMapper.selepage", map);
System.out.println(list);
session.close();
}
}
对应在xml中用#{key}或#{属性名}取值,并且要在xml对应的标签中加上parameterType属性,表示要传的参数类型(注意系统内置别名,具体查看官方文档)。对应的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="com.test.mapper.PeopleMapper">
<select id="selAll" resultType="com.test.pojo.People">
select * from people
</select>
<select id="selOne" resultType="People" parameterType="int">
select * from people where id = #{param1}
</select>
<select id="selOne1" resultType="People" parameterType="People">
select * from people where id = #{id}
</select>
<select id="selOne2" resultType="People" parameterType="map">
select * from people where id = #{id} and name = #{name}
</select>
<select id="selepage" resultType="People" parameterType="map">
select * from people limit #{startNum},#{pageSize}
</select>
<insert id="inst" parameterType="People" useGeneratedKeys="true" keyProperty="id">
insert into people values(default,#{name},#{age})
</insert>
</mapper>
6.实现增删改操作时需要手动通过session对事务进行提交
另外在插入时如果主键采用自增时在 <insert>设置两个属性:useGeneratedKeys="true" keyProperty="id"其中keyProperty为主键的列,即可将自增的主键带回(一般用于插入后向前端传回插入的对象时使用)