ma Mybatis框架
一、简单使用
准备:
新建普通maven项目
导入所需依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
重点第一步:建立全局配置文件(sqlMapConfig.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>
<!-- 配置数据库的连接信息 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=Asia/Shanghai" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml" />
<mapper resource="sqlmap/Student.xml" />
</mappers>
</configuration>
在conf下,创建sqlmap文件夹,在该文件夹下创建User.xml,用于填写sql语句,jdbc数据库操作;最好每张表对应一个.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="homework">
<!-- 查询 -->
<select id="findStudentById" parameterType="int" resultType="com.niit.entity.Student">
select * from wangzining where id = #{id}
</select>
<!-- 新增 -->
<insert id="insertStudent">
insert into wangzining(id,name,sex,phone) values(#{id},#{name},#{sex},#{phone})
</insert>
<!-- 修改 -->
<update id="updateStudent">
update wangzining set name = #{name} where id = #{id}
</update>
<!-- 删除 -->
<delete id="delStudent">
delete from wangzining where id = #{id}
</delete>
</mapper>
测试类,用法举例
@Test
public void testHomework() {
String resource = "sqlMapConfig.xml";
InputStream inputStream;
SqlSession session = null;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
//查询数据
Student student1 = session.selectOne("homework.findStudentById", 1);
System.out.println(student1);
//新增
Student student2 = new Student(4,"赵","男","12345678977");
session.insert("homework.insertStudent", student2);
session.commit();
//修改
Student student3 = new Student(5,"王","男","123456782227");
session.update("homework.updateStudent", student3);
session.commit();
//删除
session.delete("homework.delStudent", 6);
session.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
session.close();
}
}
二、高级用法(mapper代理开发)
1.新建接口,编写对数据库表的操作方法:
2.新建映射文件mapper.xml
namespace 命名空间,作用是隔离sql ,并且需要和对应的mapper接口的类路径一致
注意: 将映射文件配置在全局配置文件(sqlMapConfig.xml)中
3。测试方法(与之前不同)
- 需要注意以下几点
- 1接口中的方法名 和 对应的xml中的id 要一致
- 2 接口中方法的返回值 和对应的xml中的resultType要一致
- 3 接口中方法传入的参数,和对应xml中的parameterType 要一致