首先你要明白为什么使用ORM:我们看一个示例,我们发现我们要声明的pojo类中的属性名和数据库中的字段名不一致,这时就需要我们使用MyBatis的ORM。
首先还是准备工作:
??? 1.创建Maven工程,还没有配置Maven的和还不会的去看这里啦:maven的下载安装与配置环境变量!!!(全网最详细)-CSDN博客
Role.java:???? (pojo类)
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by.pojo;
/**
* <p>Project: mybatis - Role</p>
* <p>Powered by scl On 2023-12-20 14:36:04</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class Role {
private int id;
private String roleName;
private String roleDesc;
@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleDesc() {
return roleDesc;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
}
?2.导入依赖到pom.xml文件中
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
3.在resources中创建log4j.properites和mybatis-config.xml
log4j.properites:
log4j.properites:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
?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>
<!--使用dev环境-->
<environments default="dev">
<!--dev环境-->
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<!--使用连接池中的数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3305/mybatis?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!-- 扫描映射文件 -->
<mappers>
<mapper resource="com/by/mapper/UserMapper.xml"/>
</mappers>
</configuration>
4.创建RolerMapper接口和RoleMapper.xml文件
RolerMapper.java:
package com.by.mapper;
import com.by.pojo.Role;
import java.util.List;
/**
* <p>Project: mybatis - RoleMapper</p>
* <p>Powered by scl On 2023-12-20 14:37:16</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public interface RoleMapper {
}
?RoleMapper.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.by.mapper.RoleMapper">
</mapper>
5.创建测试类MyBatisTest
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by;
import com.by.mapper.RoleMapper;
import com.by.pojo.Role;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* <p>Project: mybatis - MyBatisTest</p>
* <p>Powered by scl On 2023-12-18 11:44:53</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class MyBatisTestRole {
private InputStream inputStream;
private SqlSession sqlSession;
@Before
public void init() throws IOException {
// 加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
// 创建sqlSessionFActory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得数据的绘画实例
sqlSession = sessionFactory.openSession();
}
@After
public void close() throws IOException {
inputStream.close();
sqlSession.close();
}
}
? 准备工作完成后,开始功能的开发:
RoleMapper.java:
package com.by.mapper;
import com.by.pojo.Role;
import java.util.List;
public interface RoleMapper {
List<Role> findAll();
}
RoleMapper.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.by.mapper.RoleMapper">
<select id="findAll" resultType="com.by.pojo.Role">
SELECT id ,ROLE_NAME roleName,ROLE_DESC roleDesc FROM role
</select>
</mapper>
测试类:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by;
import com.by.mapper.RoleMapper;
import com.by.pojo.Role;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* <p>Project: mybatis - MyBatisTest</p>
* <p>Powered by scl On 2023-12-18 11:44:53</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class MyBatisTestRole {
private InputStream inputStream;
private SqlSession sqlSession;
@Before
public void init() throws IOException {
// 加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
// 创建sqlSessionFActory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得数据的绘画实例
sqlSession = sessionFactory.openSession();
}
@Test
public void findAll(){
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
List<Role> roleList = roleMapper.findAll();
for (Role role : roleList) {
System.out.println(role);
}
}
@After
public void close() throws IOException {
inputStream.close();
sqlSession.close();
}
}
RoleMapper.java:
package com.by.mapper;
import com.by.pojo.Role;
import java.util.List;
/**
* <p>Project: mybatis - RoleMapper</p>
* <p>Powered by scl On 2023-12-20 14:37:16</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public interface RoleMapper {
List<Role> findAll2();
}
RoleMapper.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.by.mapper.RoleMapper">
<resultMap id="findAll2resultMap" type="com.by.pojo.Role">
<id column="id" property="id"></id>
<result column="role_name" property="roleName"></result>
<result column="role_desc" property="roleDesc"></result>
</resultMap>
<select id="findAll2" resultMap="findAll2resultMap">
select * from role
</select>
</mapper>
测试类:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by;
import com.by.mapper.RoleMapper;
import com.by.pojo.Role;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* <p>Project: mybatis - MyBatisTest</p>
* <p>Powered by scl On 2023-12-18 11:44:53</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class MyBatisTestRole {
private InputStream inputStream;
private SqlSession sqlSession;
@Before
public void init() throws IOException {
// 加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
// 创建sqlSessionFActory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得数据的绘画实例
sqlSession = sessionFactory.openSession();
}
@Test
public void findAll2(){
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
List<Role> roleList = roleMapper.findAll2();
for (Role role : roleList) {
System.out.println(role);
}
}
@After
public void close() throws IOException {
inputStream.close();
sqlSession.close();
}
}