jsp+springmvc+spring+mybatis+oracle

来源:互联网 发布:哔哩哔哩直播姬mac版 编辑:程序博客网 时间:2024/05/20 23:39

1,创建一个spring-mybatis-oracle这么一个javaweb工程

2,导入springmvc,web,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
         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
         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
   springmvc的jar包:
         org.springframework.web.servlet-3.0.5.RELEASE.jar
   web包:
         org.springframework.web-3.0.5.RELEASE.jar
   再加上log4j的jar包: 
         log4j-1.2.16.jar

3)创建emps.sql表,使用oracle或mysql语法

--oraclecreate table emps(eid number(5) primary key,ename varchar2(20),esal number(8,2),esex varchar2(2));
4)创建Emp.java类

/** * 员工 * @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="cn.itcast.javaee.mybatis.entity.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="cn.itcast.javaee.mybatis.entity.Emp">insert into emps(eid,ename,esal,esex) values(#{id},#{name},#{sal},#{sex})</insert></mapper>
6)创建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><mappers><mapper resource="cn/itcast/javaee/mybatis/entity/EmpMapper.xml"/></mappers></configuration>

7)创建EmpDao.java类   

/** * 持久层 * 实现类 * @author AdminTC */public class EmpDao {private SqlSessionFactory sqlSessionFactory;public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {this.sqlSessionFactory = sqlSessionFactory;}/** * 增加员工 */public void add(Emp emp) throws Exception{SqlSession sqlSession = sqlSessionFactory.openSession();sqlSession.insert("empNamespace.add",emp);sqlSession.close();}}
8)创建EmpService.java

/** * 业务层 * 实现类 * @author AdminTC */public class EmpService {private EmpDao empDao;public void setEmpDao(EmpDao empDao) {this.empDao = empDao;}/** * 注册员工 */public void register(Emp emp) throws Exception{empDao.add(emp);}}
9)创建EmpAction.java

/** * 单例 * 控制器 * @author AdminTC */@Controller@RequestMapping(value="/emp")public class EmpAction {private EmpService empService;@Resource(name="empServiceID")public void setEmpService(EmpService empService) {this.empService = empService;}/** * 注册员工 */@RequestMapping(value="/register")public String registerMethod(Emp emp) throws Exception{//调用业务层empService.register(emp);return "success";}}

10)配置spring.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: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="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">      <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>      <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>      <property name="user" value="scott"/>      <property name="password" value="tiger"/>      </bean>                              <!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->      <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">      <property name="configLocation" value="classpath:mybatis.xml"/>      <property name="dataSource" ref="comboPooledDataSourceID"/>      </bean>               <!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->      <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">      <property name="dataSource" ref="comboPooledDataSourceID"/>      </bean>                        <!-- 配置事务通知,即让哪些方法需要事务支持 -->      <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">      <tx:attributes>      <tx:method name="*" propagation="REQUIRED"/>      </tx:attributes>      </tx:advice>                                    <!-- 配置事务切面,即让哪些包下的类需要事务 -->      <aop:config>      <aop:pointcut id="pointcut" expression="execution(* cn.itcast.javaee.mybatis.dao.*.*(..))"/>     <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>      </aop:config>                        <!-- 注册EmpDao -->      <bean id="empDaoID" class="cn.itcast.javaee.mybatis.dao.EmpDao">      <property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>      </bean>                  <!-- 注册EmpService -->      <bean id="empServiceID" class="cn.itcast.javaee.mybatis.service.EmpService">      <property name="empDao" ref="empDaoID"/>      </bean>                  <!-- 注册EmpAction -->      <context:component-scan base-package="cn.itcast.javaee.mybatis.action"/>  <!-- 通知springioc容器这些注解的作用 -->      <context:annotation-config/>            <!-- 视图解析器 -->      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">      <property name="prefix" value="/jsp/"/>      <property name="suffix" value=".jsp"/>      </bean>            </beans> 
11)编写register.jsp

<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>员工注册</title>  </head>  <body><form action="${pageContext.request.contextPath}/emp/register.action" method="POST"><table border="2" align="center"><tr><th>编号</th><td><input type="text" name="id"></td></tr><tr><th>姓名</th><td><input type="text" name="name"></td></tr><tr><th>薪水</th><td><input type="text" name="sal"></td></tr><tr><th>性别</th><td><input type="radio" name="sex" value="男"/>男<input type="radio" name="sex" value="女" checked/>女</td></tr><tr><td colspan="2" align="center"><input type="submit" value="注册"/></td></tr></table></form>  </body></html>








原创粉丝点击