MyBatis框架的作用:可以简化JDBC操作,实现数据的持久化。
ORM(Object Relational Mapping):对象关系映射。将数据库中的表和实体类一一映射,可以使开发人员像操作对象一样操作数据库的表。
MyBtais可以理解为ORM的一种实现,除了MyBatis外,还有Hibernate等等。
开发MyBatis的步骤:
1、在数据库中创建表(student1)
2、创建与表对应的实体类(Student)
private int stuNo;
private String stuName;
private int stuAge;
private String graName;
//实体类的属性和表中的字段对应
3、创建实体类和表的映射文件(目前先把实体类Student和映射文件StudentMapper.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">
<!-- namespace是该映射文件的唯一标识符 -->
<mapper namespace="org.yao.entity.StudentMapper">
<!-- parameterType是输入参数的类型
resultType是返回结果值类型
-->
<select id="queryStudentById" parameterType="int" resultType="org.yao.entity.Student">
select * from student1 where stuno = #{stuno}
</select>
<!-- MyBatis约定输入参数和输出参数在形式上只能有一个 -->
<insert id="addStudent" parameterType="org.yao.entity.Student">
insert into student1(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName})
</insert>
<delete id="deleteStudentByStuno">
delete from student1 where stuno = #{stuno}
</delete>
<update id="updateStudentByStuno" parameterType="org.yao.entity.Student">
update student1 set stuname=#{stuName},stuage=#{stuAge},graname=#{graName} where stuno=#{stuNo}
</update>
<!-- 不管是否返回一个集合,返回值类型都是org.yao.entity.Student -->
<select id="queryAllStudents" resultType="org.yao.entity.Student">
select * from student1
</select>
</mapper>
4、创建配置文件
(conf.xml配置文件的作用有
1、配置数据库的信息
2、加载映射文件)
<?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>
<!-- environments的defult属性和environment的id值,可MyBatis运行时的数据库环境 -->
<!-- 可以使用SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader,"development");来指定运行时的数据库环境 -->
<environments default="development">
<!-- 开发环境 -->
<environment id="development">
<!-- 事务提交方式:
JDBC:利用JDBC方式处理事务(需要手动的写commit rollback close等)
MANAGED:将事务交由其他组件去托管(比如Spring等),默认会关闭连接,如果不要关闭连接,则需要配置
<property name="closeConnection" value="false"/>
-->
<transactionManager type="JDBC"/>
<!-- 数据源类型
UNPOOLED:使用传统的JDBC模式(每次访问数据库都要打开、关闭连接,比较耗费性能)
POOLED:使用数据库连接池,没有特殊要求,都选用POOLED
JNDI:从tomcat中获取一个内置的数据库连接池
-->
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/yq"/>
<property name="username" value="数据库用户名"/>
<property name="password" value="数据库密码"/>
</dataSource>
</environment>
<!-- 项目发布在另一个计算机上,运行(实施)环境 -->
<environment id="shishi">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/yq"/>
<property name="username" value="数据库用户名"/>
<property name="password" value="数据库密码"/>
</dataSource>
</environment>
<!-- 测试部门的运行环境,运行(实施)环境 -->
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/yq"/>
<property name="username" value="数据库用户名"/>
<property name="password" value="数据库密码"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载映射文件 -->
<mapper resource="org/yao/entity/StudentMapper.xml"/>
</mappers>
</configuration>
5、测试MyBatis程序
// 查询单个学生
public static void queryStudentByStuno() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "org.yao.entity.StudentMapper.queryStudentById";
Student student = session.selectOne(statement, 1);
System.out.println(student);
session.close();
}
// 查询全部学生
public static void queryAllStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "org.yao.entity.StudentMapper.queryAllStudents";
List<Student> students = session.selectList(statement);
System.out.println(students);
session.close();
}
// 增加学生
public static void insertStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "org.yao.entity.StudentMapper.addStudent";
Student student = new Student(3, "ww", 25, "g2");
int count = session.insert(statement, student);
// 注意要提交啊
session.commit();
System.out.println("增加" + count + "个学生");
session.close();
}
// 删除学生
public static void deleteStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "org.yao.entity.StudentMapper.deleteStudentByStuno";
int count = session.delete(statement, 3);
// 注意要提交啊
session.commit();
System.out.println("删除" + count + "个学生");
session.close();
}
// 修改学生
public static void updateStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "org.yao.entity.StudentMapper.updateStudentByStuno";
Student student = new Student();
student.setStuNo(2);
student.setStuName("lxs");
student.setStuAge(44);
student.setGraName("g1");
int count = session.update(statement, student);
// 注意要提交啊
session.commit();
System.out.println("修改" + count + "个学生");
session.close();
}
以上便是使用MyBatis框架的一个简单的增删改查。
注意:
1、如果使用的事务方式为jdbc,则需要手工commit提交,即session.commit();
2、所有标签等,都必须有sql语句,但是sql语句的参数值可选。如
sql有参数:session.insert(statement,参数值);
sql没有参数:session.insert(statement);
MyBatis的jar和源码
提取码:q6hq