MyBatis学习日记(二): MyBatis Say Hello

时间:2024-05-01 23:43:06

首先在Eclipse中创建一个maven工程:

MyBatis学习日记(二): MyBatis Say Hello

MyBatis学习日记(二): MyBatis Say Hello

在maven工程下的pom.xml文件中添加MyBatis、MySQL、Junit依赖:

 <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.Aiden</groupId>
<artifactId>MyBatisByRe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

在MySQL中创建数据库——db_mbre,并建立一个t_student表:

 CREATE TABLE `t_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在src文件夹下创建MySQL数据库链接的配置文件——jdbc.properties:

 jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mbre
jdbc.username=root
jdbc.password=123456

在src文件夹下创建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>
<properties resource="jdbc.properties" />
<typeAliases>
<typeAlias alias="Student" type="com.Aiden.domain.Student" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/Aiden/dao/StudentMapper.xml" />
</mappers>
</configuration>

在src目录下创建com.Aiden.dao、com.Aiden.domain、com.Aiden.service、com.Aiden.util四个包。

MyBatis学习日记(二): MyBatis Say Hello

在com.Aiden.domain包下创建Student实体文件——Student.java:

 package com.Aiden.doamin;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
super();
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

在com.Aiden.dao包下创建student的映射接口(StudentMapper.java)以及映射文件(StudentMapper.xml):

 package com.Aiden.dao;
import com.Aiden.doamin.Student;
public interface StudentMapper {
public int addStudent(Student student);
}
 <?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.Aiden.dao.StudentMapper">
<insert id="addStudent" parameterType="Student">
insert into t_student values(null,#{name},#{age})
</insert>
</mapper>

在com.Aiden.util包下创建SqlSession工具类——SqlSessionFactoryUtil.java:

 package com.Aiden.util;

 import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtil {
public static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory getSqlSessionFactory() {
if(sqlSessionFactory==null) {
InputStream inputStream=null;
try {
inputStream=Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sqlSessionFactory;
}
public static SqlSession openSession() {
return getSqlSessionFactory().openSession();
}
}

在com.Aiden.service包下创建Junit测试类——MybatisDemo01.java:

 package com.Aiden.service;

 import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.Aiden.dao.StudentMapper;
import com.Aiden.domain.Student;
import com.Aiden.util.SqlSessionFactoryUtil; public class MybatisDemo02 {
private static SqlSession sqlSession=null;
private static StudentMapper studentMapper=null; @Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
} @After
public void tearDown() throws Exception {
sqlSession.close();
} @Test
public void testAddStudent() {
Student student=new Student("苍井空", 26);
studentMapper.addStudent(student);
sqlSession.commit();
} }

运行测试方法testAddStudent。插入数据成功。

MyBatis学习日记(二): MyBatis Say Hello