先简单介绍下项目
一.用mybatis这个orm框架
orm工具的基本思想
无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.
2. 由sessionfactory 产生 session
3. 在session 中完成对数据的增删改查和事务提交等.
4. 在用完之后关闭session 。
5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。
二.用mysql数据库
三.实现junit测试查询学生信息
以下是步骤:
maven环境搭建略...
1---创建项目
生成项目
2.为项目增加两个resource文件夹
---项目右键--build path--configure build path
3.为pom.xml添加依赖
<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.master</groupId>
<artifactId>stu_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>stu_test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.master.App</mainClass>
</manifest>
</archive>
<descriptorRefs>jar-with-dependencies</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.创建Student模型
Student.java
package com.master;
public class Student {
private int id;
private String name;
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
}
5.创建StudentMapper.java
package com.master;
public interface StudentMapper {
Student getStudent(int id);
}
6.在src/main/resources 下创建mapper包并在mapper包创建StudentMapper.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.master.StudentMapper">
<!-- 根据id查user -->
<select id="getStudent" parameterType="int" resultType="Student">
select * from Student where id=#{id}
</select>
</mapper>
7.在src/main/resources 下创建Configuration.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.master.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://127.0.0.1:3306/mybastis_test" />
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
8.编写测试类
package com.master;
import java.io.IOException;
import java.io.Reader;
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.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
private SqlSession sqlSession = null;
@Before
public void init() throws IOException {
Reader reader = Resources.getResourceAsReader("Configuration.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(reader);
reader.close();
sqlSession = sqlSessionFactory.openSession();
}
@Test
public void testGetStudent() {
StudentMapper mapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
Student student = mapper.getStudent(1);
Assert.assertEquals("testwfq", student.getName());
}
@After
public void close() {
sqlSession.close();
}
}
附完整源代码:点击打开链接