映射配置文件包含了数据和对象之间的映射关系以及要执行的SQL语句。
- < mapper>是核心根标签,namespace是名称空间。
- < select>是查询钙能的标签,id是唯一的标识,resultType将查询结果封装为某种类型的对象,parameterType指定参数映射对象的类型,如果SQL语句中有参数需要用此属性指定。
一、映射配置文件
1.查询
在映射配置文件的< mapper>标签内编写sql语句实现通过id查询一行记录的功能,其中,#{}为占位符,传入待查询的sid字段。
<select id="selectById" resultType="com.example.bean.Student" parameterType="java.lang.Integer">
SELECT * FROM student WHERE sid=#{id}
</select>
public void selectById() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.执行映射配置文件中的SQL语句并接收结果
Student stu = sqlSession.selectOne("StudentMapper.selectById", 3);
//5.处理结果
System.out.println(stu);
//6.释放资源
sqlSession.close();
is.close();
}
2.新增
<!-- 从Student类中获取id name age -->
<insert id="insert" parameterType="com.example.bean.Student">
INSERT INTO student VALUES(#{sid}, #{name}, #{age}, #{birthday});
</insert>
public void insert() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();//默认false,需要手动提交事务,或者设置为true,则自动提交事务
//4.执行映射配置文件中的SQL语句并接收结果
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// Date nowDate = dateFormat.parse(nowdayTime);
Student stu = new Student(5, "大王", 26, dateFormat.parse("1999-09-09"));
int res = sqlSession.insert("StudentMapper.insert",stu);
//5.增删改 需要提交事务
sqlSession.commit();
//6.处理结果
System.out.println(res);
//7.释放资源
sqlSession.close();
is.close();
}
3.修改
<update id="update" parameterType="com.example.bean.Student">
UPDATE student SET name=#{name}, age=#{age}, birthday=#{birthday} WHERE sid=#{sid}
</update>
public void update() throws Exception{
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Student stu = new Student(5, "大李", 21, dateFormat.parse("2000-01-01"));
int res = sqlSession.update("StudentMapper.update", stu);
sqlSession.commit();
if (res != 0) System.out.println("修改成功!");
sqlSession.close();
is.close();
}
4.删除
<delete id="delete" parameterType="java.lang.Integer">
DELETE FROM student WHERE sid=#{sid};
</delete>
public void delete() throws Exception{
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
int res = sqlSession.delete("StudentMapper.delete", 5);
sqlSession.commit();
if(res != 0) System.out.println("删除成功!");
sqlSession.close();
is.close();
}
二、核心配置文件
1.核心配置文件主要标签介绍
核心配置文件包含了 MyBatis 最核心的设置和属性信息。如数据库的连接、事务、连接池信息等。
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration 核心根标签-->
<configuration>
<!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个-->
<environments default="mysql">
<!--environment配置数据库环境 id属性唯一标识-->
<environment id="mysql">
<!-- transactionManager事务管理。 type属性,采用JDBC默认的事务-->
<transactionManager type="JDBC"></transactionManager>
<!-- dataSource数据源信息 type属性 连接池-->
<dataSource type="POOLED">
<!-- property获取数据库连接的配置信息 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///db1" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- mappers引入映射配置文件 -->
<mappers>
<!-- mapper 引入指定的映射配置文件 resource属性指定映射配置文件的名称 -->
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
2.数据库连接配置文件的引入方式
<!-- 引入数据库连接的配置文件 -->
<properties resource="jdbc.properties"/>
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
3.起别名
<typeAliases>
<typeAlias type="com.example.bean.Student" alias="student"/>
<!-- 给包下所有的类起别名,为文件名 -->
<package name="com.example.bean"/>
</typeAliases>
内置别名:
| 别名 | 对应java数据类型 |
|---|---|
| string | java.lang.String |
| long | java.lang.Long |
| int | java.lang.Integer |
| double | java.lang.Double |
| boolean | java.lang.Boolean |
本文介绍了使用 MyBatis 框架进行基本的增删改查操作的方法,并详细展示了对应的 XML 映射文件配置及 Java 调用示例。
1621

被折叠的 条评论
为什么被折叠?



