转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662
1. 添加pom依赖:
<properties> <mybatis.version>3.2.7</mybatis.version> <mysql-connector-java.version>5.1.29</mysql-connector-java.version> <junit.version>4.11</junit.version> </properties> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> <type>jar</type> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
2. 建库建表(mysql)
create database football DEFAULT CHARACTER SET utf8; use football; create table players( id int primary key auto_increment, name varchar(20) not null comment '球员名', age int )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='球员表'; insert into players(name, age) values('kaka', 33); insert into players(name, age) values('ronaldo', 40);
3. mybatis基础配置 (mybatis-base.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> <environments default="development"> <environment id="development"> <!-- 事务使用的是jdbc的方式 --> <transactionManager type="JDBC" /> <!-- 连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/football" /> <property name="username" value="root" /> <property name="password" value="your password" /> </dataSource> </environment> </environments> </configuration>
1. transaction: JDBC – 这个配置就是直接使用了 JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务范围。 MANAGED – 这个配置几乎没做什么。它从来不提交或回滚一个连接,而是让容器来管理事务的整个生命周期(比如 JEE 应用服务器的上下文)。 如果是spring+mybatis,无需配置transactionManager。 2. dataSource type: UNPOOLED|POOLED|JNDI
4. 定义实体类
package com.sohu.tv.bean; /** * 用户实体 * @author leifu * @Date 2015年7月28日 * @Time 上午9:48:09 */ public class Player implements Serializable{ /** * 用户id */ private int id; /** * 用户名 */ private String name; /** * 年龄 */ private int age; public Player() { } public Player(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Player [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
5. 定义Dao
package com.sohu.tv.mapper; import com.sohu.tv.bean.Player; /** * 注解方式实现PlayerDao * * @author leifu * @Date 2015年7月28日 * @Time 上午10:16:39 */ public interface PlayerDao { public Player getPlayerById(int id); }
6. 定义操作players表的sql映射文件playerMapper.xml (放到classpath下 mapper目录下)
<?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.sohu.tv.mapper.PlayerDao"> <select id="getPlayerById" parameterType="int" resultType="com.sohu.tv.bean.Player"> select id,name,age from players where id=#{id} </select> </mapper>
7. 在mybatis-base.xml文件中注册playerMapper.xml文件
<mappers> <!--xml方式--> <mapper resource="mapper/playerMapper.xml" /> </mappers>
8. 单元测试(最原始方式)
package com.sohu.tv.test.mapper; 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.Test; import com.sohu.tv.bean.Player; import com.sohu.tv.mapper.PlayerDao; /** * mybatis-xml方式配置(原始方式) * * @author leifu * @Date 2015年7月28日 * @Time 上午9:54:07 */ public class PlayerMapperXmlTest { @Test public void testGetPlayer() throws IOException { String resource = "mybatis-base.xml"; // 加载mybatis的配置文件(它也加载关联的映射文件) Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); // 创建能执行映射文件中sql的sqlSession SqlSession sqlSession = sessionFactory.openSession(true); //statement方式 String statement = "com.sohu.tv.mapper.PlayerDao" + ".getPlayerById"; Player player1 = sqlSession.selectOne(statement, 1); System.out.println("player1: " + player1); //Mapper代理方式 PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); Player player2 = playerDao.getPlayerById(2); System.out.println("player2: " + player2); } }