1. 工程主要文件
2. 主要jar包
mybatis-3.2.0.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-3.1.13-bin.jar
log4j-1.2.11.jar
3. mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
<configuration> <properties resource="resources/jdbc.properties"></properties> <typeAliases> <!-- 导入此包下的所有类,相当于注册。。 --> <package name="com.edu.mybatis_spring.model" /> </typeAliases>
<environments default="development"> <environment id="development"> <!-- 使用JDBC的事务管理 --> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> </dataSource> </environment> </environments>
<mappers> <mapper resource="com/edu/mybatis_spring/mapper/UserMapper.xml" /> </mappers> </configuration> |
4. UserMapper.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.edu.mybatis_spring.mapper.UserMapper"> <!-- 当使用该Mybatis与Spring整合的时候, xml文件必须和相应的Mapper接口文件同名,并在同一路径下 --> <insert id="addUser" parameterType="User"> insert into tb_user(name, age) values(#{name}, #{age}) </insert>
<select id="getUserByName" resultType="User" parameterType="java.lang.String"> select * from tb_user where name=#{name} </select> </mapper> |
5. User.java
package com.edu.mybatis_spring.model;
public class User { private int id; private String name; private int age; / 省略getter/setter方法
|
6. UserMapper.java
package com.edu.mybatis_spring.mapper;
import com.edu.mybatis_spring.model.User;
public interface UserMapper { public void addUser(User user); public User getUserByName(String username); }
|
7. UserDaoImpl.java
package com.edu.mybatis_spring.dao.impl;
import org.apache.ibatis.session.SqlSession;
import com.edu.mybatis_spring.mapper.UserMapper; import com.edu.mybatis_spring.model.User; import com.edu.mybatis_spring.util.MybatisUtil;
public class UserDaoImpl implements UserMapper {
@Override public void addUser(User user) { SqlSession session = null;
try { session = MybatisUtil.getSession(); session.getMapper(UserMapper.class).addUser(user); session.commit(); } catch (Exception e) { session.rollback(); e.printStackTrace(); } finally { MybatisUtil.closeSession(session); } }
@Override public User getUserByName(String username) { return MybatisUtil.getSession().getMapper(UserMapper.class) .getUserByName(username); }
} |
8. MybatisUtil.java
package com.edu.mybatis_spring.util;
import java.io.IOException; import java.io.InputStream;
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder
public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory = null; / initialized SqlSessionFactory static { try { InputStream is = Resources .getResourceAsStream("resources/mybatis-config.xml");
sqlSessionFactory = new
SqlSessionFactoryBuilder } catch (IOException e) { e.printStackTrace(); } }
public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; }
public static SqlSession getSession() { return sqlSessionFactory.openSession(); }
public static void closeSession(SqlSession session) { session.close(); } }
|
9. jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis_spring?useUnicode=true&characterEncoding=UTF-8 username=root password=mysql123 |
10. Init.sql
drop database mybatis_spring; create database mybatis_spring; use mybatis_spring;
drop table tb_user; create table tb_user( id int auto_increment, name varchar(30), age int(10), primary key(id) );
insert into tb_user(id, name, age) values(null, 'zhangsan', 20); |
11. UserDaoImplTest
package com.edu.mybatis_spring.dao.impl;
import org.junit.After; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test;
import com.edu.mybatis_spring.mapper.UserMapper; import com.edu.mybatis_spring.model.User;
public class UserDaoImplTest {
private UserMapper userMapper = null;
@Before public void setUp() throws Exception { userMapper = new UserDaoImpl(); }
@After public void tearDown() throws Exception { userMapper = null; }
@Test public void testAddUser() { User user = new User(); user.setName("lisi"); user.setAge(12); userMapper.addUser(user); assertNotNull("=========== userMapper.addUser(user) failure...", userMapper.getUserByName("lisi")); }
@Test public void testGetUserByName() { assertNotNull(userMapper.getUserByName("lisi")); }
}
|
12. 结果
|
|
12. 分析总结
1. http://mybatis.github.io/mybatis-3/zh/
版权声明:本文为博主原创文章,未经博主允许不得转载。