----------------------------映射文件----重要的是插入一条记录---------------------------------------
<?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="cn.gdpe.bean.Emps">
<resultMap type="emps" id="empsMap">
<id property="id" column="id"/>
<result property="name" column="ename"/>
<result property="sex" column="esex"/>
<result property="sal" column="esal"/>
</resultMap>
<insert id="addEmp" parameterType="emps" useGeneratedKeys="false">
<selectKey resultType="int" keyProperty="id" order="BEFORE">
select EMPS_SEQ.nextval as id from dual
</selectKey>
insert into emps(id,ename,esex,esal) values(#{id},#{name},#{sex},#{sal})
</insert>
<!-- <insert id="addEmp" parameterType="emps" keyProperty="id" useGeneratedKeys="false">
insert into emps(id,ename,esex,esal) values(EMPS_SEQ.nextval,#{name},#{sex},#{sal})
</insert> -->
<select id="findAll" resultMap="empsMap">
select id,ename,esex,esal from emps;
</select>
<select id="findById" parameterType="int" resultMap="empsMap">
select id,ename,esex,esal from emps where id =#{id}
</select>
</mapper>
--------------------------------mybaits.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>
<!-- 资源文件 -->
<properties resource="db.properties"/>
<typeAliases>
<typeAlias type="cn.gdpe.bean.Emps" alias="emps"/>
</typeAliases>
<environments default="oracle_development">
<environment id="mysql_development">
<!--mybatis的事务管理方式 -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
<environment id="oracle_development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${oracle.driver}"/>
<property name="url" value="${oracle.url}"/>
<property name="username" value="${oracle.username}"/>
<property name="password" value="${oracle.password}"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="cn/gdpe/bean/EmpsMapper.xml"/>
</mappers>
</configuration>
--------------------------------------资源文件-----------------------------------------------------
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://127.0.0.1:3306/day15
mysql.username=root
mysql.password=root
oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@127.0.0.1 :1521:orcl
oracle.username=scott
oracle.password=root
---------------------------------------------springmvc配置文件---------------------------------
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="cn.gdpe"></context:component-scan>
<!-- 注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
上面一句代码,就已经帮我们做了这一步,所以有了context:component-scan 就省略这一步
-->
<context:annotation-config />
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1 :1521:orcl"></property>
<property name="user" value="scott"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置数据工厂 加载mybatis配置文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- 配置 -->
<!-- 配置事务管理器 mybatis底层用的是jdbc事务管理器 不知道是不是这里必须配置JDBC事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务通知器 即哪些方法需要事务 -->
<tx:advice id="tx" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="NEVER"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<!-- 切点 -->
<aop:pointcut id="emp" expression="execution(* cn.gdpe.service.*.* (..))" />
<!-- 关联事务通知器与切点 -->
<aop:advisor advice-ref="tx" pointcut-ref="emp"/>
</aop:config>
<!-- 注解方法适配器 主要解决json-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
</beans>
--------------------------------web.xml配置文件----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
---------------------------------------工具类---------两个------------------------------------
package cn.gdpe.util;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtil {
private static DataSource dataSource;
static{
dataSource=new ComboPooledDataSource();
}
public static DataSource getDataSource(){
return dataSource;
}
public static QueryRunner getQueryRunner(){
return new QueryRunner(dataSource);
}
}
package cn.gdpe.util;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil {
private static ThreadLocal<SqlSession> threadLocal=new ThreadLocal<SqlSession>();
private static SqlSessionFactory sqlSessoinFactory;
private MybatisUtil(){}
static{
InputStream is=MybatisUtil.class.getResourceAsStream("/mybatis.xml");
sqlSessoinFactory=new SqlSessionFactoryBuilder().build(is);
}
public static SqlSession getSqlSession(){
//从当前线程取出sqlsession
SqlSession sqlSession=threadLocal.get();
//如果为空
if(sqlSession==null){
//就从sqlSessoinFactory中打开一个sqlSession
sqlSession=sqlSessoinFactory.openSession();
//将session与当前线程绑在一起
threadLocal.set(sqlSession);
}
return sqlSession;
}
public static void closeSqlSession(){
//从当前线程取出sqlsession
SqlSession sqlSession=threadLocal.get();
//如果不为空
if(sqlSession!=null){
//将session与当前线程分开
sqlSession.close();
threadLocal.remove();
}
}