MyBatis日记(二): MyBatis Say Hello
首先在Eclipse中创建一个maven工程:
在maven工程下的pom.xml文件中添加MyBatis、MySQL、Junit依赖:
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.Aiden</groupId>
6 <artifactId>MyBatisByRe</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <dependencies>
9 <dependency>
10 <groupId>org.mybatis</groupId>
11 <artifactId>mybatis</artifactId>
12 <version>3.5.1</version>
13 </dependency>
14 <dependency>
15 <groupId>mysql</groupId>
16 <artifactId>mysql-connector-java</artifactId>
17 <version>8.0.15</version>
18 </dependency>
19 <dependency>
20 <groupId>junit</groupId>
21 <artifactId>junit</artifactId>
22 <version>4.12</version>
23 <scope>test</scope>
24 </dependency>
25 </dependencies>
26 </project>
在MySQL中创建数据库——db_mbre,并建立一个t_student表:
1 CREATE TABLE `t_student` (
2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `name` varchar(50) DEFAULT NULL,
4 `age` int(10) DEFAULT NULL,
5 PRIMARY KEY (`id`)
6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在src文件夹下创建MySQL数据库链接的配置文件——jdbc.properties:
1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/db_mbre
3 jdbc.username=root
4 jdbc.password=123456
在src文件夹下创建MyBatis的配置文件——MyBatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6 <properties resource="jdbc.properties" />
7 <typeAliases>
8 <typeAlias alias="Student" type="com.Aiden.domain.Student" />
9 </typeAliases>
10 <environments default="development">
11 <environment id="development">
12 <transactionManager type="JDBC" />
13 <dataSource type="POOLED">
14 <property name="driver" value="${jdbc.driverClassName}" />
15 <property name="url" value="${jdbc.url}" />
16 <property name="username" value="${jdbc.username}" />
17 <property name="password" value="${jdbc.password}" />
18 </dataSource>
19 </environment>
20 </environments>
21 <mappers>
22 <mapper resource="com/Aiden/dao/StudentMapper.xml" />
23 </mappers>
24 </configuration>
在src目录下创建com.Aiden.dao、com.Aiden.domain、com.Aiden.service、com.Aiden.util四个包。
在com.Aiden.domain包下创建Student实体文件——Student.java:
1 package com.Aiden.doamin;
2 public class Student {
3 private Integer id;
4 private String name;
5 private Integer age;
6 public Student() {
7 super();
8 }
9 public Student(String name, Integer age) {
10 super();
11 this.name = name;
12 this.age = age;
13 }
14 public Student(Integer id, String name, Integer age) {
15 super();
16 this.id = id;
17 this.name = name;
18 this.age = age;
19 }
20 public Integer getId() {
21 return id;
22 }
23 public void setId(Integer id) {
24 this.id = id;
25 }
26 public String getName() {
27 return name;
28 }
29 public void setName(String name) {
30 this.name = name;
31 }
32 public Integer getAge() {
33 return age;
34 }
35 public void setAge(Integer age) {
36 this.age = age;
37 }
38 @Override
39 public String toString() {
40 return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
41 }
42
43 }
在com.Aiden.dao包下创建student的映射接口(StudentMapper.java)以及映射文件(StudentMapper.xml):
1 package com.Aiden.dao;
2 import com.Aiden.doamin.Student;
3 public interface StudentMapper {
4 public int addStudent(Student student);
5 }
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.Aiden.dao.StudentMapper">
6 <insert id="addStudent" parameterType="Student">
7 insert into t_student values(null,#{name},#{age})
8 </insert>
9 </mapper>
在com.Aiden.util包下创建SqlSession工具类——SqlSessionFactoryUtil.java:
1 package com.Aiden.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import org.apache.ibatis.io.Resources;
6 import org.apache.ibatis.session.SqlSession;
7 import org.apache.ibatis.session.SqlSessionFactory;
8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
9
10 public class SqlSessionFactoryUtil {
11 public static SqlSessionFactory sqlSessionFactory;
12 public static SqlSessionFactory getSqlSessionFactory() {
13 if(sqlSessionFactory==null) {
14 InputStream inputStream=null;
15 try {
16 inputStream=Resources.getResourceAsStream("mybatis-config.xml");
17 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
18 } catch (IOException e) {
19 // TODO Auto-generated catch block
20 e.printStackTrace();
21 }
22 }
23 return sqlSessionFactory;
24 }
25 public static SqlSession openSession() {
26 return getSqlSessionFactory().openSession();
27 }
28 }
在com.Aiden.service包下创建Junit测试类——MybatisDemo01.java:
1 package com.Aiden.service;
2
3 import org.apache.ibatis.session.SqlSession;
4 import org.junit.After;
5 import org.junit.Before;
6 import org.junit.Test;
7
8 import com.Aiden.dao.StudentMapper;
9 import com.Aiden.domain.Student;
10 import com.Aiden.util.SqlSessionFactoryUtil;
11
12 public class MybatisDemo02 {
13 private static SqlSession sqlSession=null;
14 private static StudentMapper studentMapper=null;
15
16 @Before
17 public void setUp() throws Exception {
18 sqlSession=SqlSessionFactoryUtil.openSession();
19 studentMapper=sqlSession.getMapper(StudentMapper.class);
20 }
21
22 @After
23 public void tearDown() throws Exception {
24 sqlSession.close();
25 }
26
27 @Test
28 public void testAddStudent() {
29 Student student=new Student("苍井空", 26);
30 studentMapper.addStudent(student);
31 sqlSession.commit();
32 }
33
34 }
运行测试方法testAddStudent。插入数据成功。