1)创建一个spring-mybaits-oracle这么一个javaweb或java工程
2)导入spring,mybatis,c3p0,oracle和mybatis提供的与spring整合的插件包
2)导入spring,mybatis,c3p0,oracle和mybatis提供的与spring整合的插件包
mysql的jar:
mysql-connector-java-5.1.7-bin.jar
oracle的jar:
ojdbc5.jar
c3p0的jar:
c3p0-0.9.1.2.jar
mybatis的jar:
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
mybatis-3.1.1.jar
mybatis与spring整合的jar
【mybatis-spring-1.1.1.jar】
spring的ioc模块的jar:
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
commons-logging.jar
spring的aop模块的jar:
aopalliance.jar
aspectjweaver.jar
cglib-2.2.2.jar
org.springframework.aop-3.0.5.RELEASE.jar
spring的transaction模块的jar:
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
3)创建emps.sql表,使用oracle或mysql语法
--oracle
create table emps(
eid number(5) primary key,
ename varchar2(20),
esal number(8,2),
esex varchar2(2)
);
4)创建Emp.java类
package cn.buaa.javaee.entity;
/**
* 员工
* @author AdminTC
*/
public class Emp {
private Integer id;
private String name;
private Double sal;
private String sex;
public Emp(){}
public Emp(Integer id, String name, Double sal, String sex) {
this.id = id;
this.name = name;
this.sal = sal;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
5)创建EmpMapper.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="empNamespace">
<resultMap type="emp" id="empMap">
<id property="id" column="eid"/>
<result property="name" column="ename"/>
<result property="sal" column="esal"/>
<result property="sex" column="esex"/>
</resultMap>
<!-- 增加员工 -->
<insert id="add" parameterType="emp">
insert into emps(eid,ename,esal,esex) values(#{id},#{name},#{sal},#{sex})
</insert>
</mapper>
6)创建mybatis.xml配置文件,注意,因为spring配置了连接池的管理,所以mybatis中连接池配置删除即可(下边为了测试,没删除,不删除,默认用的也是老大Spring)
<?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>
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/>
<!-- 设置类型别名 -->
<typeAliases >
<typeAlias type="cn.buaa.javaee.entity.Emp" alias="emp"/>
</typeAliases>
<!-- 设置默认的连接环境信息 -->
<environments default="oracle_developer">
<!-- 连接环境信息 ,随便起一个唯一的名字 -->
<environment id="mysql_developer">
<!-- mybatis使用jdbc事物管理方式 -->
<transactionManager type="jdbc"></transactionManager>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
<!-- 连接环境信息 ,随便起一个唯一的名字 -->
<environment id="oracle_developer">
<!-- mybatis使用jdbc事物管理方式 -->
<transactionManager type="jdbc"></transactionManager>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521/orcl"/>
<property name="username" value="zhangdong"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件-->
<mappers>
<mapper resource="cn/buaa/javaee/entity/EmpMapper.xml"/>
</mappers>
</configuration>
7)创建EmpDao.java类
package cn.buaa.javaee.dao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import cn.buaa.javaee.entity.Emp;
public class EmpDao {
private SqlSessionFactory sqlSessionFactory;
//ioc容器自动注入
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
/**
* 增加员工
*/
public void add(Emp emp) throws Exception{
/*SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
sqlSession.insert("empNamespace.add", emp);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}*/
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("empNamespace.add", emp);
sqlSession.close();
}
}
package cn.buaa.javaee.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.buaa.javaee.dao.EmpDao;
import cn.buaa.javaee.entity.Emp;
/**
* 单元测试
* @author 梧桐下的茵
*
*/
public class TestEmpDao {
//单独测试mybatis
@Test
public void test1() throws Exception{
EmpDao empDao = new EmpDao();
empDao.add(new Emp(1,"哈哈",7000D,"男"));
}
//测试spring整合mybatis
@Test
public void test2() throws Exception{
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
EmpDao empDao = (EmpDao)ac.getBean("empDao");
empDao.add(new Emp(3,"哈哈",7000D,"男"));
}
}
《-------------------------------------------------------------更新添加springmvc--------------------------------------------------------------------------------->
注意:下面加入springmvc(action控制层)
9) 1、加入springmvc相关的jar,在上面基础上加两个包就行了
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
2,配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 核心springmvx核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、控制器action
package cn.buaa.javaee.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.buaa.javaee.entity.Emp;
import cn.buaa.javaee.service.EmpService;
/**
* 控制器
* @author 梧桐下的茵
*
*/
@Controller
@RequestMapping(value="/emp")
public class EmpAction {
private EmpService empService;
@Resource
public void setEmpService(EmpService empService) {
this.empService = empService;
}
/**
* 注册员工
*/
@RequestMapping(value="/register")
public String register (Emp emp) throws Exception{
empService.register(emp);
return "success";
}
}
4、spring.xml中注册dao,service,action,扫描注解啊,视图解析器啊,等等
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 配置c3p0连接池-->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521/orcl"/>
<property name="user" value="zhangdong"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置SqlSessionFactoryBean 目的:加载mybatis配置文件和映射文件,即替代mybatis工具类的作用-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 配置Mybatis的事务管理器,即因为mybatis底层用的是JDBC事务管理器,所以在这里依然配置JDBC事物管理 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 配置事物通知-->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 配置事物切面,即让那些包下的类需要事物-->
<aop:config>
<aop:pointcut expression="execution(* cn.buaa.javaee.dao.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
</aop:config>
<!-- 注册EmpDao -->
<bean id="empDao" class="cn.buaa.javaee.dao.EmpDao">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
<!-- 注册EmpService -->
<bean id="empService" class="cn.buaa.javaee.service.EmpService">
<property name="empDao" ref="empDao"/>
</bean>
<!-- 注册action -->
<context:component-scan base-package="cn.buaa.javaee.action"></context:component-scan>
<!-- 通知springioc容器这些注解的作用 -->
<context:annotation-config/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>