mybatis hellworld
用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 表
![](https://images2015.cnblogs.com/blog/709529/201511/709529-20151101164126232-729834724.png)
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 (名字可以随意)
-
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 对实体类进行设置别名,别名主要是用于映射文件上的,可以看UserDao.xml文件的resultType的值,该值就是用到这里的别名 --> 6 <typeAliases> 7 <typeAlias alias="User" type="cn.jamkong.mybatisDemo.domain.User"/> 8 </typeAliases> 9 <!-- 配置环境 --> 10 <environments default="mysql"> 11 <environment id="mysql"> 12 <!-- 使用JDBC来管理事务 --> 13 <transactionManager type="JDBC"/> 14 <!-- 设置数据源 --> 15 <dataSource type="POOLED"> 16 <property name="driver" value="com.mysql.jdbc.Driver"/> 17 <property name="url" value="jdbc:mysql://localhost:3306/test"/> 18 <property name="username" value="root"/> 19 <property name="password" value="root"/> 20 </dataSource> 21 </environment> 22 </environments> 23 <!-- 映射文件 --> 24 <mappers> 25 <mapper resource="mapping/UserDao.xml"/> 26 </mappers> 27 </configuration>
6. 写Dao的映射文件:如UserDao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="cn.jamkong.mybatisDemo.dao.UserDao"> 5 <resultMap type="User" id="userMap"> 6 <id property="id" column="id"/> 7 <result property="name" column="name"/> 8 <result property="age" column="age"/> 9 <result property="address" column="address"/> 10 </resultMap> 11 12 <select id="list" resultType="User" resultMap="userMap"> 13 <![CDATA[ 14 SELECT * FROM `USER` 15 ]]> 16 </select> 17 </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()); } } }