使用方式
创建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"/> <!--开启蛇形命名法到驼峰命名法映射,解决数据库也Java对象命名的不统一-->
</settings>
<environments default="development"> <!--环境配置,default代表指定当前环境-->
<environment id="development"> <!--开发配置,id指定当前是开发环境-->
<transactionManager type="JDBC"/> <!--数据库连接方式使用JDBC-->
<dataSource type="POOLED"> <!--数据源使用数据库连接池方式连接-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/> <!--数据库驱动-->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/> <!--数据库连接URL-->
<property name="username" value="root"/> <!--数据库用户名-->
<property name="password" value="root"/> <!--数据库密码-->
</dataSource>
</environment>
</environments>
<mappers>
<!--映射配置,共有三种配置方式-->
</mappers>
</configuration>
xml映射方式
- 创建一个实体类,映射数据库中的表,创建映射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="student"> <!--该映射的命名空间-->
<select id="selectAll" resultType="com.Student"> <!--该命名空间下配置的SQL,每个SQL都有一个ID作为唯一标识,方便定位到该SQL-->
<!--resultType属性标识该SQL返回对象数据类型,使用类的全类名即可,返回对象数据类型并非返回值类型-->
select * from students
</select>
</mapper>
- 并在MyBatis全局配置文件中的
mappers
中添加映射
<mappers>
<mapper resource="mappers/studentMapper.xml"></mapper>
<!--resource属性指定映射文件路径,一般映射文件比较多可单独创建一个包存放,使用斜杠分隔符分隔路径,而不是点-->
</mappers>
- 编写程序进行使用
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); //MyBatis提供的读取配置文件方法读取配置,抛出IO异常
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //建造者模式构建工厂
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession(); //创建sqlSession对象
List<Student> students = sqlSession.selectList("student.selectAll"); //执行查询方法
//MyBatis会根据命名空间和ID找配置的映射文件中的SQL执行
System.out.println(students); //输出结果
} catch (Exception e) {
e.printStackTrace();
} finally { //释放资源
if (sqlSession != null) {
sqlSession.close();
}
}
}
接口映射方式
- 创建一个实体类,映射数据库中的表,再创建一个映射接口,一般使用映射的类名加
Mapper
后缀
public interface StudentMapper {
List<Student> selectAll();
}
- 创建映射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.StudentMapper"> <!--绑定接口时的命名空间就应该是该接口的全类名-->
<select id="selectAll" resultType="com.Student"> <!--绑定接口时的ID就应该是该接口中的方法-->
select * from students
</select>
</mapper>
- 并在MyBatis全局配置文件中的
mappers
中添加映射
<mappers>
<mapper resource="mappers/studentMapper.xml"></mapper> <!--resource属性指定映射文件路径-->
<!-- <mapper class="com.StudentMapper"></mapper >-->
<!--class属性指定映射接口全类名,要求映射xml文件必须与接口同一目录且文件名也相同,但是要注意下面的坑-->
</mappers>
要注意在Mavan项目下,编译时不会处理源代码目录中的配置文件,需要在资源目录中创建相同的包才能被处理,也可以在pom.xml
中增加下面配置,让Mavan也处理源代码目录
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 编写程序进行使用
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); //MyBatis提供的读取配置文件方法读取配置,抛出IO异常
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //建造者模式构建工厂
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession(); //创建sqlSession对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//使用getMapper()方法加载映射接口,MyBatis会使用该接口创建一个代理对象
List<Student> students = mapper.selectAll(); //使用该代理对象执行映射文件中的SQL
System.out.println(students); //输出结果
} catch (Exception e) {
e.printStackTrace();
} finally { //释放资源
if (sqlSession != null) {
sqlSession.close();
}
}
}
注解映射方式
- 创建一个实体类,映射数据库中的表,再创建一个映射接口,一般使用映射的类名加
Mapper
后缀,在该接口方法上添加注解替代映射xml
public interface StudentMapper {
@Select("select * from students")
List<Student> selectAll();
}
- 并在MyBatis全局配置文件中的
mappers
中添加映射,必须使用mapper
的class
属性进行配置该接口的全类名
<mappers>
<mapper class="com.StudentMapper"></mapper >
</mappers>
- 编写程序进行使用
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); //MyBatis提供的读取配置文件方法读取配置,抛出IO异常
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //建造者模式构建工厂
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession(); //创建sqlSession对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//使用getMapper()方法加载映射接口,MyBatis会使用该接口创建一个代理对象
List<Student> students = mapper.selectAll(); //使用该代理对象执行映射接口方法中注解里的SQL
System.out.println(students); //输出结果
} catch (Exception e) {
e.printStackTrace();
} finally { //释放资源
if (sqlSession != null) {
sqlSession.close();
}
}
}
Comments NOTHING