Mybatis学习之简单使用

最近在项目中使用了Mybatis,之前也自己玩过一点点。使用和自己玩的过程中总会遇到这样那样的问题,
需要好久才能找到解决的办法。当然最主要还是对Mybatis框架源码不熟。现在终于空下来一点,可以对
Mybatis框架源码的学习进行整理记录。从今天的01篇开始,主要是从创建一个maven项目、配置Mybatis
文件并初步使用Mybatis基本功能。

创建maven项目,命名为MybatisDemo。

pom文件中加入mybatis的jar包和mysql的jar包。

<!-- 添加mybatis的核心包 -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.1</version>
</dependency>
<!-- 添加mysql驱动包 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
</dependency>
<!-- 添加junit单元测试jar包 -->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

创建com.hyc.domain包,创建Student实体

public class Student {
	private Integer studId;
	private String name;
	private String email;
	// getter setter method
	...
}

创建com.hyc.mappers包,创建StudentMapper实体,定义相关方法

public interface StudentMapper {
	
	List<Student> findAllStudents();
		
	Student findStudentById(Integer id);
		
	void insertStudent(Student student);
}

创建StudentMapper.xml映射文件,配置mapper方法的sql语句

<?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.hyc.mappers.StudentMapper">
	<resultMap type="Student" id="StudentResult">
		<id property="studId" column="stud_id" />
		<result property="name" column="name" />
		<result property="email" column="email" />
	</resultMap>
	<select id="findAllStudents" resultMap="StudentResult">
		SELECT * FROM STUDENT
	</select>
	<select id="findStudentById" parameterType="int" resultType="Student">
		SELECT STUD_ID AS STUDID, NAME, EMAIL FROM STUDENT WHERE STUD_ID=#{Id}
	</select>
	<insert id="insertStudent" parameterType="Student">
		INSERT INTO STUDENT(STUD_ID,NAME,EMAIL) VALUES (#{studId },#{name},#{email})
	</insert>
</mapper>

创建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>
	<typeAliases>
		<typeAlias alias="Student" type="com.hyc.domain.Student" />
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
		   		<property name="url" 
				value="jdbc:mysql://localhost:3306/test" />
		   		<property name="username" value="root" />
		   		<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/hyc/mappers/StudentMapper.xml" />
	</mappers>
</configuration>

至此,简单的使用配置已经配置完成,后续文章再对每个配置做细致分析。接下来写测试代码。

测试代码

新建StudentMapperTest类,添加ini方法和相关测试方法

private SqlSession sqlSession;
@Before
public void init() throws IOException {
    InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"development");
    sqlSession = sqlSessionFactory.openSession();
}
	
@Test
public void findAllStudents() throws Exception {
    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
    System.out.println(studentMapper.findAllStudents());
}

@Test
public void findStudentById() throws Exception {
	StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	System.out.println(studentMapper.findStudentById());
}

@Test
public void insertStudent() throws Exception {
	StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	Student student = new Student("test");
	System.out.println(studentMapper.insertStudent(student));
}

结束

至此,一个可测试的MybatisDemo已经生成,后续文章会结合配置和源码进行学习。

更新时间:2020-12-13 11:40:54

本文由 HycJack 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://www.hycjack.cn/archives/mybatis学习01
最后更新:2020-12-13 11:40:54

评论

Your browser is out of date!

Update your browser to view this website correctly. Update my browser now

×