Mybatis
作用:简化JDBC操作,实现数据的持久化。
ORM:对象关系映射(Object Relational Mapping),mybatis是ORM的一种实现。即可以操作对象一样操作数据库。
配置
映射文件mapper.xml 增删改查标签
conf.xml 配置数据库信息和需要加载的映射文件
映射文件配置
xxMapping.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="StudentMapping">
<select id="queryAllStudent" resultType="Student">
select * from Student
</select>
<select id="queryStudentBySno" resultType="Student" parameterType="int">
select * from Student where sno = ${sno}
</select>
</mapper>
<mapper namespace="StudentMapping">
namespace用于绑定dao层接口(面对接口编程)
<select id="queryStudentBySno" resultType="Student" parameterType="int">
id用于标识一个sql标签,resultType为返回类型,parameterType为传参类型,statementType为 statement 类型,默认为 PREPARED(STATEMENT,CALLABLE)并用 #{} 或 ${} 占位。
(resultType和parameterType)只能有一个。
#{xx}
会自动为xx加上单引号,如果parameterType是简单类型(包括String)xx命名无要求,如果是对象属性必须严格命名,(#{sName}),可以防注入
${xx}
不会加单引号,适合动态字段,动态排序。在某些版本,简单类型xx必须要求为value(${value})
conf.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>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="studentMapping.xml"/>
</mappers>
</configuration>
<environments default="development">
<environment id="development">
环境配置,通过default 来选择不同的环境,即可以配置多个数据库
<transactionManager type="JDBC"/>
事务管理器 JDBC 事务需手动提交
<mapper resource="studentMapping.xml"/>
关联相关映射文件
db.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=scott
password=tiger
两种使用方式
导入config.xml并获取SqlSessionFactory
String resource = "config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
第一种
通过手动输入映射文件的namespace.id指向对应的sql操作
try (SqlSession session = sqlSessionFactory.openSession()) {
Student studnet = (Student) session.selectOne("StudentMapping.queryStudentBySno", 2);
}
第二种(约定方式,面向接口)
约定:
1.namespace 为接口的全类名
2.接口方法名为sql标签id值
3.接口方法返回值为resultType,参数为parameterType
StudentMapping接口
import java.util.HashMap;
import java.util.List;
public interface StudentMapping {
List<Student> queryAllStudent();
Student queryStudentBySno(int sno);
void insertStudent(Student student);
void updateStudentBySno(Student student);
int deleteStudentBySno(int sno);
StudentTest queryStudentBySnoByHashMap(int sno);
List<Student> queryStudentBySnoOrSname(HashMap hashMap);
List<Student> queryStudentBySnoArray(int[] sno);
}
try (SqlSession session = sqlSessionFactory.openSession()) {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student studnet = (Student) session.selectOne(2);
}