主要功能:执行数据库的操作,完成对数据的增删改查,看作是jdbc的升级版本
mybatis配置文件
<!-- settings:控制mybatis全局行为 -->
<settings>
<!-- 配置日志功能 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!--配置 mybatis 环境-->
<environments default="mydev">
<!--id:数据源的名称-->
<environment id="mydev">
<!--配置事务类型:使用 JDBC 事务(使用 Connection 的提交和回滚)-->
<transactionManager type="JDBC"/>
<!--数据源 dataSource:创建数据库 Connection 对象
type: POOLED 使用数据库的连接池
-->
<dataSource type="POOLED">
<!--连接数据库的四个要素-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bjpowernode"/>
<property name="username" value="root"/>
<property name="password" value="333"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--告诉 mybatis 要执行的 sql 语句的位置-->
<!--一个mapper标签指定一个文件位置,从类路径开始(target/classes)-->
<mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
</mappers>
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">
<!-- 指定约束文件,mybatis-3-mapper.dtd是约束文件名称,属性要符合mybatis的规则 -->
<!--
namespace:必须有值,自定义的唯一字符串
推荐使用:dao 接口的全限定名称
-->
<mapper namespace="com.bjpowernode.dao.StudentDao">
</mapper>
@Test
public void testInsert() throws IOException {
//定义mybatis主配置文件名称,从类路径的根开始(target/classes)
String config="mybatis.xml";
//读取config表示的文件
InputStream in = Resources.getResourceAsStream(config);
//创建SqlSessionFactoryBuilder对象,用于创建SqlSessionFactory对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//创建SqlSessionFactory对象(重量级对象)
SqlSessionFactory factory = builder.build(in);
//从SqlSessionFactory 获取 SqlSession,SqlSession 能执行 sql 语句
//true:自动提交事务;无参和false:不自动提交事务,需要手动提交事务sqlsession.commit();
SqlSession sqlsession = factory.openSession(true);
//执行 SqlSession 的 insert()
Student student = new Student();
student.setId(1004);
student.setName("刘备");
student.setEmail("liubei@qq.com");
student.setAge(22);
int nums = sqlsession.insert("com.bjpowernode.dao.StudentDao.insertStudent",student);
System.out.println("执行结果="+nums);
//关闭 SqlSession,释放资源
sqlsession.close();
}
上述代码可以封装,使用工具类MybatisUtils,直接获取SqlSession
==================================================================
使用mybatis动态代理机制
@Test
public void testInsertStudent(){
SqlSession sqlsession = MyBatisUtils.getSqlSession();
//使用mybatis动态代理机制,使用SqlSession.getMapper
//getMapper能获取dao接口对于的实现类对象
//dao:jdk的动态代理
StudentDao dao = sqlsession.getMapper(StudentDao.class);
Student student = new Student();
student.setId(1006);
student.setName("张飞");
student.setEmail("zhangfei@qq.com");
student.setAge(23);
//调用dao方法执行数据库操作
int nums = dao.insertStudent(student);
System.out.println("执行结果="+nums);
sqlsession.close();
}
表的列名和java对象的属性名不一致
1.使用resultMap指定列名和属性名称相对应
<!--定义resultMap
id:自定义名称,表示定义的这个resultMap
type:java类型全限定名称-->
<resultMap id="myStudentMap" type="com.bjpowernode.domain.MyStudent">
<!--column:列名,property:java类型的属性名-->
<id column="id" property="id"/>
<!--非主键用result-->
<!--自定义指定,但是数据类型需相同-->
<result column="name" property="email"/>
<result column="email" property="name"/>
<result column="age" property="age"/>
</resultMap>
2.使用resultType通过别名的方式,指定别名和属性名称相对应
模糊查询
<!--模糊查询第一种-->
<select id="selectLikeOne" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name like #{name}
</select>
<!--模糊查询第二种-->
<select id="selectLikeTwo" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name like "%" #{name} "%"
</select>
动态sql
<!--创建 sql 片段 id:片段的自定义名称-->
<sql id="studentSql">
select id,name,email,age from student
</sql>
1.if
<!--在where后面加id > 0 或者 1=1 避免后面语句出错,如where or age > ?-->
<select id="selectStudentIf" resultType="com.bjpowernode.domain.Student">
<include refid="studentSql"/>
where id > 0
<if test="name!=null | name!='' ">
and name = #{name}
</if>
<if test="age > 0">
and age > #{age}
</if>
</select>
得到:select id,name,email,age from student where id > 0 and name = #{name} and age > #{age}
2.where(常用)
<!--有查询条件时,可以自动添加上 where 子句;没有查询条件时,不会添加 where 子句,并且自动去除多余的and or 等-->
<select id="selectStudentWhere" resultType="com.bjpowernode.domain.Student">
<include refid="studentSql"/>
<where>
<if test="name !=null | name !='' ">
name = #{name}
</if>
<if test="age > 0">
or age > #{age}
</if>
</where>
</select>
得到:select id,name,email,age from student where name = #{name} or age > #{age}
3.foreach
<select id="selectStudentForeachOne" resultType="com.bjpowernode.domain.Student">
<include refid="studentSql"/> where id in
<foreach collection="list" item="myid" open="(" close=")" separator=",">
#{myid}
</foreach>
</select>
collection :值有三个,分别是list、array、map,分别对应的参数类型为:List、数组、map集合
item : 表示在迭代过程中每一个元素的别名
index :表示在迭代过程中每次迭代到的位置(下标)
open :前缀
close :后缀
separator :分隔符,表示迭代时每个元素之间以什么分隔
得到:select id,name,email,age from student where id in (#{myid})
4.补充:#{}和${}区别
mybatis主配置文件定义别名
mybatis主文件指定mapper方式