简单的测试表:
create table User(
userId int(10) primary key auto_increment,
username varchar(20) default null
)
实体类:
package org.mybatis.jpetstore.domain;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Integer userId;
private String username;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User [userId=" + userId + ", username=" + username + "]";
}
}
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="org.mybatis.jpetstore.persistence.UserMapper"> <select id="insertUser" parameterType="User"> insert into user(username) values(#{username}) </select> <select id="deleteUser" parameterType="User"> delete from user where userId = #{userId} </select> <select id="updateUser" parameterType="User"> update user u set u.username = #{username} where u.userId=#{userId} </select> <select id="selectUser" resultType="User"> select * from user </select> </mapper>
UserMapper.java:
package org.mybatis.jpetstore.persistence;
import java.util.List;
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.jpetstore.domain.Item;
import org.mybatis.jpetstore.domain.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserMapper extends SqlSessionDaoSupport{
public List<Item> getItem() {
return getSqlSession().selectList("org.mybatis.jpetstore.persistence.ItemMapper.getAllItems");
}
public void insert(User user) throws Exception{
getSqlSession().insert("org.mybatis.jpetstore.persistence.UserMapper.insertUser",user);
}
public void delete(User user) throws Exception{
getSqlSession().delete("org.mybatis.jpetstore.persistence.UserMapper.deleteUser",user);
}
public void update(User user) throws Exception{
getSqlSession().update("org.mybatis.jpetstore.persistence.UserMapper.updateUser",user);
}
public List<User> select() throws Exception{
return getSqlSession().selectList("org.mybatis.jpetstore.persistence.UserMapper.selectUser");
}
@Override
@Resource(name="sqlSessionFactory")
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
System.out.println(" ----------------- set sqlSessionFactory :"+ sqlSessionFactory);
super.setSqlSessionFactory(sqlSessionFactory);
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 启动注入功能 -->
<context:annotation-config />
<!-- 启动扫描component功能 -->
<context:component-scan base-package="org.mybatis.jpetstore" />
<!-- 启动注解实物配置功能 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定UserMapper.xml位置 -->
<property name="mapperLocations" value="classpath*:org/mybatis/jpetstore/persistence/UserMapper.xml" />
<!-- 指定别名 -->
<property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
测试类:
package com.test.ibatis;
import java.util.List;
import org.junit.Test;
import org.mybatis.jpetstore.domain.User;
import org.mybatis.jpetstore.persistence.UserMapper;
import org.mybatis.jpetstore.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
@Test
public void insert() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userDao = (UserMapper)context.getBean("userDao");
User user = new User();
user.setUsername("AAA");
userDao.insert(user);
}
@Test
public void delete() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userDao = (UserMapper)context.getBean("userDao");
User user = new User();
user.setUserId(1);
userDao.delete(user);
}
@Test
public void update() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userDao = (UserMapper)context.getBean("userDao");
User user = new User();
user.setUserId(2);
user.setUsername("改名字");
userDao.update(user);
}
@Test
public void select() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userDao = (UserMapper)context.getBean("userDao");
List<User> users = userDao.select();
for(User u : users){
System.out.println(u);
}
}
}
OK。。。