Mybatis-Mapper接口方式
目录
配置方式和接口方式的区别
示例:Mapper接口方式实现数据库的CRUD
配置方式和接口方式的区别
Mybatis框架在配置方式的情况下,对数据库的CRUD操作直接通过SqlSession对象来操作,常用的方法有select、insert、update、delete等方法,在这些方法的参数中,需要准确的写明对应xml文件的namespace+方法,参考下图的代码方式:
Mybatis接口方式(推荐方式)实现数据库的CRUD操作,采用接口方式开发,根据接口里面的方法实现数据库的操作,基于这种方式实现,需要如下约定:
- mapper文件中的namespace名称就是接口的包名+接口名称
- mapper文件中的方法需要和接口的方法名保持一致
示例:Mapper接口方式实现数据库的CRUD
Mybatis Mapper接口方式实现数据库操作有如下步骤
1、pom添加mybatis等依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
2、Mybatis配置(mybatis-config.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>
<settings>
<!-- 驼峰命名转换 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="com.text.datasource.C3P0DataSourceFactory">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://xxx:3306/xxx?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
<property name="initialPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="minPoolSize" value="5"/>
</dataSource>
</environment>
</environments>
<!-- mapper文件-->
<mappers>
<mapper resource="mappers/student.xml"/>
</mappers>
</configuration>
3、配置mapper文件 student.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="com.text.mapper.StudentMapper">
<select id="selectAll" resultType="com.text.entity.Student">
select * from student
</select>
</mapper>
注意: xml文件中的namespace一定是下方StudentMapper接口类的完整路径名,id就是StudentMapper接口类的方法名
4、编写Mapper接口 StudentMapper
package com.text.mapper;
import com.text.entity.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAll() throws Exception;
}
5、测试类
package com.text;
import com.text.entity.Student;
import com.text.mapper.StudentMapper;
import com.text.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class Application {
public static void main(String[] args) throws Exception {
SqlSession sqlSession = MyBatisUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectAll();
students.forEach(student -> {
System.out.println(student);
});
MyBatisUtils.closeSession(sqlSession);
}
}
注意:通过sqlSession的getMapper直接获取了Mapper接口,通过直接调用接口里面的方法进而对数据库进行操作
6、运行结果: