用maven来进行搭建项目的~~
1. 搭建环境 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>cn.jamkong</groupId>
<artifactId>simpleMybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>simpleMybatis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
2. 创建数据库 test
test数据库中的 user 表
3. 创建实体User
public class User {
private int id;
private String name;
private String age;
private String address;
//省略get set方法
}
4. 创建Dao接口 UserDao
public interface UserDao {
//查询所有User
public List<User> list();
}
5. 进入主题:配置mybatis的配置文件: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>
<!-- 对实体类进行设置别名,别名主要是用于映射文件上的,可以看UserDao.xml文件的resultType的值,该值就是用到这里的别名 -->
<typeAliases>
<typeAlias alias="User" type="cn.jamkong.mybatisDemo.domain.User"/>
</typeAliases>
<!-- 配置环境 -->
<environments default="mysql">
<environment id="mysql">
<!-- 使用JDBC来管理事务 -->
<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="mapping/UserDao.xml"/>
</mappers>
</configuration>
6. 写Dao的映射文件:如UserDao.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="cn.jamkong.mybatisDemo.dao.UserDao">
<resultMap type="User" id="userMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="address" column="address"/>
</resultMap> <select id="list" resultType="User" resultMap="userMap">
<![CDATA[
SELECT * FROM `USER`
]]>
</select>
</mapper>
7. 现在已经都搭建好了,准备测试:
package mybatisDemo;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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.Test;
import cn.jamkong.mybatisDemo.domain.User;
/**
* 测试类
*
* @author Administrator
*
*/
public class Test1 {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
@Test
public void test() {
SqlSession session = getSession();
List<User> list = session.selectList("cn.jamkong.mybatisDemo.dao.UserDao.list");
for (int i = 0; i < list.size(); i++) {
System.out.println("id:" + list.get(i).getId() + " name:" + list.get(i).getName() + " age:"
+ list.get(i).getAge() + " address:" + list.get(i).getAddress());
}
}
}