需要的jar包:
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
log4j-1.2.17.jar
mybatis-3.2.8.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-4.1.3.RELEASE.jar
spring-aspects-4.1.3.RELEASE.jar
spring-beans-4.1.3.RELEASE.jar
spring-context-4.1.3.RELEASE.jar
spring-context-support-4.1.3.RELEASE.jar
spring-core-4.1.3.RELEASE.jar
spring-expression-4.1.3.RELEASE.jar
spring-instrument-4.1.3.RELEASE.jar
spring-instrument-tomcat-4.1.3.RELEASE.jar
spring-jdbc-4.1.3.RELEASE.jar
spring-jms-4.1.3.RELEASE.jar
spring-messaging-4.1.3.RELEASE.jar
spring-orm-4.1.3.RELEASE.jar
spring-oxm-4.1.3.RELEASE.jar
spring-test-4.1.3.RELEASE.jar
spring-tx-4.1.3.RELEASE.jar
spring-web-4.1.3.RELEASE.jar
spring-webmvc-4.1.3.RELEASE.jar
spring-webmvc-portlet-4.1.3.RELEASE.jar
spring-websocket-4.1.3.RELEASE.jar
1、新建表
create table s_user1(
user_id int auto_increment primary key,
user_name varchar(20),
user_birthday date,
user_salary double
);
2、创建实体类
package model;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
private double salary;
public User() {
super();
}
public User(int id, String name, Date birthday, double salary) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.salary = salary;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
+ ", salary=" + salary + "]";
}
}
3、创建mapper接口
package inter;
import java.util.List;
import model.User;
public interface UserMapper {
public void save(User u);
public void delete(int id);
public void update(User u);
public User findById(int id);
public List<User> findAll();
}
4、编写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="inter.UserMapper">
<insert id="save" parameterType="model.User">
insert into s_user(user_name,user_birthday,user_salary) values(#{name},#{birthday},#{salary})
</insert>
<update id="update" parameterType="model.User">
update s_user set user_name=#{name}, user_birthday=#{birthday}, user_salary=#{salary} where user_id=#{id}
</update>
<delete id="delete" parameterType="int">
delete from s_user where user_id=#{id}
</delete>
<select id="findById" parameterType="int" resultType="model.User">
select user_id as id,user_name as name,user_birthday as birthday,user_salary as salary
from s_user
where user_id=#{id}
</select>
<select id="findAll" resultType="model.User">
select user_id as id,user_name as name,user_birthday as birthday,user_salary as salary
from s_user
</select>
</mapper>
5、编写数据库配置db.properties
jdbc.username=root
jdbc.password=huxc
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.driver=com.mysql.jdbc.Driver
6、编写spring配置文件beans.xml
<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源 -->
<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="huxc"></property>
-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:inter/*.xml"></property>
<property name="typeAliasesPackage" value="model"></property><!-- 扫描model包自动生成别名 -->
</bean>
<!-- 使用MapperScannerConfigurer时会导致表达式${jdbc.url}等失效,所以属性里需要用sqlSessionFactoryBeanName -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- <property name="sqlSessionFactory" ref="sessionFactory"></property> -->
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
<property name="basePackage" value="inter"></property><!-- 自动扫描sql映射文件的接口包inter -->
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* *.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
7、编写mybatis.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>
</configuration>
8、编写测试类
package test;
import inter.UserMapper;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import model.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoTest {
private UserMapper userMapper;
@Test
public void save() throws ParseException{
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
userMapper=(UserMapper) ac.getBean("userMapper");
User u=new User();
u.setName("Lucy");
u.setBirthday(getDate());
u.setSalary(5000);
userMapper.save(u);
}
@Test
public void update() throws ParseException{
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
userMapper=(UserMapper) ac.getBean("userMapper");
User u=new User();
u.setId(2);
u.setName("Jack");
u.setBirthday(getDate());
u.setSalary(6000);
userMapper.update(u);
}
@Test
public void delete(){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
userMapper=(UserMapper) ac.getBean("userMapper");
userMapper.delete(3);
}
@Test
public void findById(){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
userMapper=(UserMapper) ac.getBean("userMapper");
User u=userMapper.findById(1);
System.out.println(u);
}
@Test
public void findAll(){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
userMapper=(UserMapper) ac.getBean("userMapper");
List<User> users=userMapper.findAll();
for(User u:users){
System.out.println(u);
}
}
public Date getDate() throws ParseException{
String date="2014-5-2 12:34:56";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse(date);
return d;
}
}