1.jar包
2.beans.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ywdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource / typeAliasesPackage -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="typeAliasesPackage" value="com.yw.springmybatis.bean" />
</bean>
<!-- 3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer sqlSessionFactory
/ basePackage -->
<!-- <bean id="config" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yw.springmybatis.mapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> -->
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="manager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource" />
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="manager" />
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.yw.springmybatis.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
3.mybatis-config.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>
<!-- Spring整合myBatis后,这个配置文件基本可以不要了 -->
<!-- 设置外部配置文件 -->
<!-- 设置类别名 -->
<!-- 设置数据库连接环境 -->
<!-- 映射文件 -->
</configuration>
4.
package com.yw.springmybatis.bean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
private double salary;
public User(int id, String name, Date birthday, double salary) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.salary = salary;
}
public User() {
super();
}
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() {
SimpleDateFormat sdm=new SimpleDateFormat("yyyy-MM-dd");
String sbirthday=sdm.format(birthday);
return "User [id=" + id + ", name=" + name + ", sbirthday=" + sbirthday
+ ", salary=" + salary + "]";
}
}
5.userMpper.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">
<!--namespace必须是接口的全类名 -->
<mapper namespace="com.yw.springmybatis.mapper.UserMapper">
<resultMap type="User" id="userResult">
<result column="user_id" property="id"/>
<result column="user_name" property="name"/>
<result column="user_birthday" property="birthday"/>
<result column="user_salary" property="salary"/>
</resultMap>
<!-- 取得插入数据后的id -->
<insert id="save" keyColumn="user_id" keyProperty="id" useGeneratedKeys="true">
insert into s_user(name,birthday,salary)
values(#{name},#{birthday},#{salary})
</insert>
<update id="update">
update s_user
set name = #{name},
birthday = #{birthday},
salary = #{salary}
where id = #{id}
</update>
<delete id="delete">
delete from s_user
where id = #{id}
</delete>
<select id="findById" resultMap="userResult">
select *
from s_user
where id = #{id}
</select>
<select id="findAll" resultMap="userResult">
select *
from s_user
</select>
</mapper>
6.UserMapper.java
package com.yw.springmybatis.mapper;
import java.util.List;
/*
* 约定
*/
import com.yw.springmybatis.bean.User;
public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List<User> findAll();
}
7.
package com.yw.springmybatis.test;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yw.springmybatis.bean.User;
import com.yw.springmybatis.mapper.UserMapper;
//使用Springtest框架
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/beans.xml")
// 加载配置
public class SMTest
{
@Autowired
private UserMapper mapper;
@Test
public void testAdd()
{
System.out.println("mapper=" + mapper);
User user = new User(-1, "tom", new Date(), 1234);
mapper.save(user);
//
// int id = user.getId();
// System.out.println(id);
}
@Test
public void testFindAll()
{
System.out.println("mapper=" + mapper);
// User user = new User(-1, "tom", new Date(), 1234);
// mapper.save(user);
List list = mapper.findAll();
System.out.println("list=" + list);
//
// int id = user.getId();
// System.out.println(id);
}
public static void main(String[] args)
{
ApplicationContext ctx = null;
ctx = new ClassPathXmlApplicationContext("beans.xml");
UserMapper um=(UserMapper)ctx.getBean("userDao");
System.out.println("um="+um);//org.apache.ibatis.binding.MapperProxy@7215fb38
List list = um.findAll();
System.out.println("list=" + list);
UserMapper um2=(UserMapper)ctx.getBean("userDao");
System.out.println("um2="+um2);//org.apache.ibatis.binding.MapperProxy@7215fb38
User user = new User(-1, "tom111111111111111", new Date(), 1234);
// mapper.save(user);
// um2.save(user);
// um2.delete(-1);
um2.delete(3);
// System.out.println(1/0);
List list3 = um.findAll();
System.out.println("list3=" + list3);
// //beans.xml将Mapper接口生成代理注入到Spring
// Object obj = ctx.getBean("userMapper");//org.apache.ibatis.binding.MapperProxy
// System.out.println("obj="+obj);
// UserMapper um=(UserMapper)obj;
// System.out.println("um="+um);
// List list = um.findAll();
// System.out.println("list=" + list);
}
}
8.运行如图