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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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">
<!-- 1. 数据源:DriverManagerDataSource -->
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="mysql" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>
<!-- 2. mybatis的sqlSession的工厂:SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- datasource: 引用数据源 -->
<property name="dataSource" ref="ds" />
<!-- 别名包,设置之后自动扫描包下的类 -->
<property name="typeAliasesPackage" value="com.stone.bean" />
</bean>
<!-- 3. mybatis自动扫描加载sql映射文件:MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- basePackage: 指定Sql映射文件/接口所在的包,自动扫描 -->
<property name="basePackage" value="com.stone.dao" />
<!-- sqlSessionFactory 引用定义好的sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 4. 事务管理:DataSourceTransactionManager -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- dataSource 引用上面定义的数据源 -->
<property name="dataSource" ref="ds" />
</bean>
<!-- 5.使用声明式事务 -->
<!-- transaction-manager : 引用上面定义的事务管理器 -->
<tx:annotation-driven transaction-manager="txManager" /> </beans>

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> </configuration>

Java Bean

package com.stone.bean;

import java.text.SimpleDateFormat;
import java.util.Date; public class Person { private int id;
private String name;
private Date birthday; public Person() {
super();
} public Person(int id, String name, Date birthday) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
} 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;
} @Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:SS");
return "Person [id=" + id + ", name=" + name + ", birthday="
+ dateFormat.format(birthday) + "]";
} }

dao - 两个文件名必须一样

package com.stone.dao;

import java.util.List;

import com.stone.bean.Person;

public interface PersonMapper {
void save(Person person); void update(Person person); void delete(int id); Person findById(int id); List<Person> findAll();
}
<?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.stone.dao.PersonMapper">
<!-- id:必须与对应接口的某个对应的方法一致 -->
<insert id="save" parameterType="Person">
insert into
person(name,birthday)
values(#{name},#{birthday})
</insert>
<update id="update" parameterType="Person">
update person set
name=#{name},birthday=#{birthday}
where id=#{id}
</update>
<delete id="delete" parameterType="int">
delete from person where
id=#{id}
</delete>
<select id="findById" parameterType="int" resultType="Person">
select
id,name,birthday from person where id=#{id}
</select>
<select id="findAll" resultType="Person">
select id,name,birthday from
person
</select>
</mapper>

test

package com.stone.dao.test;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.stone.bean.Person;
import com.stone.dao.PersonMapper; // 使用Spring的测试框架
@RunWith(SpringJUnit4ClassRunner.class)
// 加载spring的配置文件beans.xml
@ContextConfiguration("/beans.xml")
public class SMTest { @Autowired
private PersonMapper personMapper; @Test
public void testAdd() {
Person person = new Person(-1, "smName", new Date());
personMapper.save(person);
}
}

classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="lib/cglib-nodep-3.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="lib/mybatis-3.2.8.jar"/>
<classpathentry kind="lib" path="lib/mybatis-spring-1.2.2.jar" sourcepath="D:/stono/javasoft/mybatis/spring-mybatis-spring-1.2.2.zip"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.7-bin.jar"/>
<classpathentry kind="lib" path="lib/spring-aop-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-beans-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-context-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-core-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-expression-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-jdbc-4.1.4.RELEASE.jar" sourcepath="D:/stono/javasoft/spring/spring-framework-4.1.4.RELEASE/libs/spring-jdbc-4.1.4.RELEASE-sources.jar"/>
<classpathentry kind="lib" path="lib/spring-test-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-tx-4.1.4.RELEASE.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

MyBatis与Spring集成的更多相关文章

  1. 重构Mybatis与Spring集成的SqlSessionFactoryBean(1)

    一般来说,修改框架的源代码是极其有风险的,除非万不得已,否则不要去修改.但是今天却小心翼翼的重构了Mybatis官方提供的与Spring集成的SqlSessionFactoryBean类,一来是抱着试 ...

  2. Mybatis与Spring集成时都做了什么?

    Mybatis是java开发者非常熟悉的ORM框架,Spring集成Mybatis更是我们的日常开发姿势. 本篇主要讲Mybatis与Spring集成所做的事情,让读过本文的开发者对Mybatis和S ...

  3. mybatis与Spring集成(Aop整合PagerAspect插件)

    目的: Mybatis与spring集成 Aop整合pagehelper插件 Mybatis与spring集成 导入pom依赖 <?xml version="1.0" enc ...

  4. Mybatis与Spring集成(易百教程)

    整个Mybatis与Spring集成示例要完成的步骤如下: 1.示例功能描述 2.创建工程 3.数据库表结构及数据记录 4.实例对象 5.配置文件 6.测试执行,输出结果 1.示例功能描述 在本示例中 ...

  5. 重构Mybatis与Spring集成的SqlSessionFactoryBean(2)

    三.代码重构 1.先使用Eclipse把buildSqlSessionFactory()方法中众多的if换成小函数 protected SqlSessionFactory buildSqlSessio ...

  6. 深入浅出mybatis之与spring集成

    目录 写在前面 详细配置 1.dataSource(数据源) 2.sqlSessionFactory(Session工厂) 3.Mapper(映射器) 4.TransactionManager(事务管 ...

  7. MyBatis从入门到精通(第9章):Spring集成MyBatis(中)

    MyBatis从入门到精通(第9章):Spring集成MyBatis(中) 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法.应该将应用自身的设计和具体 ...

  8. 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”

    在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...

  9. Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成

    MyBatis-Spring它是MyBatis子模块框.它用来提供流行的依赖注入框架Spring无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向切面编程 ...

随机推荐

  1. php url 伪静态

    手册上说:   'PATH_INFO'    包含由客户端提供的.跟在真实脚本名称之后并且在查询语句(query string)之前的路径信息,如果存在的话.例如,如果当前脚本是通过 URL http ...

  2. reactor 类库,基于事件编程

    https://github.com/reactor https://github.com/reactor/reactor-samples/ https://github.com/ReactiveX/ ...

  3. codeforces 492E. Vanya and Field(exgcd求逆元)

    题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...

  4. learn from 德国老师

    最近在跟踪德国来的一个老师学android,感触比较深的一点就是他对细节的理解,一个源代码他可以从第一行解释到最后一行,知道每一行的意思和用法,这可能就是德国人对细节的追求. 刚才想了一下写代码确实应 ...

  5. JS监听input框的回车事件、属性值改变事件

    一.介绍 在程序猿门的工作中,经常会遇见一些表单数据的提交,对于有表单的东西而言,input输入框是非常常见的,所以对于一些input的基本事件,我们需要去掌握 二.input的监听enter事件 比 ...

  6. php登录

    if ($name && $passowrd){ $sql = "SELECT * FROM liuyanban WHERE name = '$name' and passw ...

  7. MAC 10.10解决 PHP GD库的问题

    1. http://yangjunwei.com/a/1570.html 2. http://php-osx.liip.ch

  8. 使用for循环运算

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Redis状态和信息查看

    转自:http://my.oschina.net/tongyufu/blog/405612 redis-cli连接服务器后,使用info命令查看Redis信息和状态: INFO [section] 以 ...

  10. (中等) POJ 2948 Martian Mining,DP。

    Description The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site ...