1.环境准备

导包(jdk1.8只支持spring4.0以上的版本

  mysql驱动包

  c3p0驱动包

  mybatis包

  spring-core、spring-aop、spring-web、spring-orm、spring-jdbc

  jackson(spring对json的支持包)

  mybatis_spring包

2.测试spring-mybatis整合

applicationContext.xml

   <!-- 配置数据源,记得去掉myBatis-config.xml的数据源相关配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/mybatis" />
<property name="user" value="root" />
<property name="password" value="juaner767" />
</bean>
<!-- 配置session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:myBatis-config.xml" />
</bean> <!-- 配置事务管理器,管理数据源事务处理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务通知 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 默认只处理运行时异常,可加rollback-for="Exception/Throwable"等处理所有异常或包括错误 -->
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes> </tx:advice>
<!-- 配置切面织入的范围,后边要把事务边界定在service层 -->
<!-- 把事务边界定在service层 -->
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution(* com.juaner.scm.dao.impl.*.*(..))"/>
</aop:config>
<context:component-scan base-package="*"></context:component-scan>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

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>
<!-- 通过别名简化对类的使用 -->
<typeAliases>
<typeAlias type="com.juaner.scm.entity.Dept" alias="Dept"/>
</typeAliases>
<mappers>
<mapper resource="com/juaner/scm/entity/DeptMapper.xml" />
</mappers>
</configuration>

Dept实体及映射文件

public class Dept implements Serializable{

    private Integer deptId;
private String deptName;
private String deptAddress; public Dept(){ }
public Integer getDeptId() {
return deptId;
} public void setDeptId(Integer deptId) {
this.deptId = deptId;
} public String getDeptName() {
return deptName;
} public void setDeptName(String deptName) {
this.deptName = deptName;
} public String getDeptAddress() {
return deptAddress;
} public void setDeptAddress(String deptAddress) {
this.deptAddress = deptAddress;
} @Override
public String toString() {
return "Dept{" +
"deptId=" + deptId +
", deptName='" + deptName + '\'' +
", deptAddress='" + deptAddress + '\'' +
'}';
}
}
<?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="com.juaner.scm.entity.DeptMapper">
<resultMap id="deptResultMap" type="Dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<result property="deptAddress" column="dept_address"/>
</resultMap>
<select id="selectDept" parameterType="Integer" resultMap="deptResultMap">
SELECT DEPT_ID,DEPT_NAME,DEPT_ADDRESS FROM DEPT WHERE DEPT_ID=#{deptID}
</select>
<insert id="insertDept" parameterType="Dept">
INSERT INTO DEPT (DEPT_NAME,DEPT_ADDRESS) VALUES (#{deptName},#{deptAddress})
</insert>
</mapper>

DeptDaoImpl类

@Repository("deptDao")
public class DeptDaoImpl implements DeptDao{
@Resource
private SqlSessionTemplate sqlSessionTemplate; /**
* 根据部门编号查询部门信息
* @param deptId 部门编号
* @return 部门信息
*/
public Dept selectDept(Integer deptId){
Dept dept= sqlSessionTemplate.selectOne("com.juaner.scm.entity.DeptMapper.selectDept", deptId);
return dept;
}
/**
* 添加部门信息
* @param dept 部门信息
* @return 添加成功的记录数
*/
public int insertDept(Dept dept){
System.out.println("------dao.dept:"+dept);
sqlSessionTemplate.insert("com.juaner.scm.entity.DeptMapper.insertDept", dept);
// int i =1/0;
return 0;
} }

测试

    @Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DeptDaoImpl deptDao = (DeptDaoImpl) context.getBean("deptDao");
Dept dept = deptDao.selectDept(1);
System.out.println(dept);
}
@Test
public void test2(){
Dept dept = new Dept();
dept.setDeptName("信息部");
dept.setDeptAddress("b3");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DeptDaoImpl deptDao = (DeptDaoImpl) context.getBean("deptDao");
deptDao.insertDept(dept);
}

3.测试spring-mvc

web.xml

   <!--配置spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置spring-mvc核心控制器-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--针对post请求的编码过滤器-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!--同时开启json的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="*"/>
</beans>

DeptAction

@Controller
@RequestMapping("/dept")
public class DeptAction {
@Resource
private DeptDaoImpl deptDao; @RequestMapping(value="/insert")
public String insert(Dept dept){
System.out.println("---action.dept:"+dept);
deptDao.insertDept(dept);
return "forward:/jsp/main.jsp";
} }

index.jsp

  <form action="dept/insert.action" method="post">
名称:<input type="text" name="deptName"><br>
地址:<input type="text" name="deptAddress"><br>
<input type="submit" value="ok">
</form>

main.jsp(webRoot/jsp/main.jsp)

4.优化

对service的事务控制(需要配置applicationContext.xml和spring-mvc.xml的扫描规则,否则会产生冲突无法回滚)

    <!-- 把事务边界定在service层 -->
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution(* com.juaner.scm.service.impl.*.*(..))"/>
</aop:config>
<!-- 自动扫描组件,要把controller去除,他们是在spring-mvc.xml中配置,如果不去除会影响事务管理。 -->
<context:component-scan base-package="com.juaner">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

spring-mvc扫描

    <!-- 扫描所有的controller 但是不扫描service -->
<context:component-scan base-package="com.juaner">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

去掉dao实现类

扫描basePackage下的接口,如果接口的强类名与映射文件xml中的命名空间一致,spring会生成该接口的代理对象,就不用写实现类了。

spring会用接口的强类名做命名空间,方法名做sql语句id查询map文件

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="basePackage" value="com.juaner.scm.dao"/>
</bean>

映射文件

<mapper namespace="com.juaner.scm.dao.DeptDao">
<resultMap id="deptResultMap" type="Dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<result property="deptAddress" column="dept_address"/>
</resultMap>
<select id="selectDept" parameterType="Integer" resultMap="deptResultMap">
SELECT DEPT_ID,DEPT_NAME,DEPT_ADDRESS FROM DEPT WHERE DEPT_ID=#{deptID}
</select>
<insert id="insertDept" parameterType="Dept">
INSERT INTO DEPT (DEPT_NAME,DEPT_ADDRESS) VALUES (#{deptName},#{deptAddress})
</insert>

dao接口

public interface DeptDao {
public Dept selectDept(Integer deptId);
public int insertDept(Dept dept);
}

aciton

    @Autowired
private DeptService deptService;

去掉myBatis-config.xml中的别名和映射文件

在applicationContext.xml中配置映射文件地址

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:myBatis-config.xml" />
<property name="mapperLocations" value="classpath:com/juaner/scm/dao/*.xml"/>
</bean>

配置别名,entity下的所有类都以类名作为别名

    <typeAliases>
<package name="com.juaner.scm.entity"/>
</typeAliases>

 5.测试json

引入jackson包jackson-core-asl-1.9.11、jackson-mapper-asl-1.9.11

spring-mvc开启对json的支持

    <mvc:annotation-driven></mvc:annotation-driven>

或在spring-mvc中配置json转换器

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>

方法中添加注解@ResponseBody

    @RequestMapping("/doAjax")
@ResponseBody
public Object doAjax(Supplier supplier){
System.out.println("-------doAjax.supplier"+supplier);
supplier.setSupName("supName1");
return supplier;
}

jsp方法中调用

    <script type="text/javascript">
$(function(){
$("#buttonID").click(
function(){
var data = {"supId":1001,"supName":"name1001"};
$.post("${proPath}/supplier/doAjax.action",data,
function(json){
alert(json.supId+" "+json.supName);
});
}
);
})
</script>

6.抽取dao、service、action、jsp

ssi项目(1)环境搭建的更多相关文章

  1. Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建

    1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...

  2. Eclipse4.6(Neon) + Tomcat8 + MAVEN3.3.9 + SVN项目完整环境搭建

    软件清单 jdk-8u102-windows-x64.exe eclipse-inst-win64.exe (Eclipse4.6 Neon) apache-tomcat-8.5.5-windows- ...

  3. Hadoop项目开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 可详细参考,一定得去看 HBase 开发环境搭建(Eclipse\MyEclipse + Maven) Zookeeper项目开发环境搭建(Eclipse\MyEclipse + Mav ...

  4. Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 可详细参考,一定得去看 HBase 开发环境搭建(Eclipse\MyEclipse + Maven) Zookeeper项目开发环境搭建(Eclipse\MyEclipse + Mav ...

  5. SSM 框架-03-MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建

    SSM 框架-03-MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建 如果你是使用 Eclipse 你需要先安装 MyEclipse,请参考:SSM 框架-02-MyEclipse ...

  6. java项目测试环境搭建

    java项目测试环境搭建 2019-03-06 13:45:26 木瓜小少年 阅读数 691更多 分类专栏: 测试   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原 ...

  7. Linux(三)—— 项目部署环境搭建

    目录 项目部署环境搭建 一.linux上网 二.rpm包管理 1.光盘挂载 2.安装卸载rpm包 3.查询是否安装 4.查看软件包 5.互相依赖关系的软件包 三.yum包管理 1.使用aliyun的y ...

  8. day76:luffy:项目前端环境搭建&轮播图的实现

    目录 1.项目前端环境搭建 1.创建项目目录 2.前端初始化全局变量和全局方法 3.跨域CORS 4.axios配置 2.轮播图功能的实现 1.安装依赖模块 2.上传文件相关配置 3.注册home子应 ...

  9. 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的

    感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...

  10. spring+springMVC+mybatis的框架项目基础环境搭建

    上一个项目在后台用到spring+springMVC+mybatis的框架,先新项目初步需求也已经下来,不出意外的话,应该也是用这个框架组合. 虽然在之前activiti相关的学习中所用到的框架也是这 ...

随机推荐

  1. CocoaPods的安装和使用

    一. CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具.在开发iOS应用时,会经常使用第三方类库,比如SDWebImage.AFNetworking等等,手动的下载与 ...

  2. php 5.3新增的闭包语法介绍function() use() {}

    * 下面提到的代码在PHP5.3以上版本运行通过. */function callback($callback) { $callback();}//输出: This is a anonymous fu ...

  3. 用SignalR实现的弹幕功能

    弹幕功能通常用于实时显示当前视频或者文档的评论内容,在上快速飞过的方式呈现,看起来比较酷炫. 这种典型的多用户实时交互的功能,很适合使用SignalR实现,通过SignalR提供后台的服务推送功能,客 ...

  4. 底部漂浮DIV

    .buttonDiv{background-color: #4e4f50; border: 2px solid #83ABD3; border-radius: 4px; -khtml-opacity: ...

  5. 转!!java事务的处理

    java的事务处理,如果对数据库进行多次操作,每一次的执行或步骤都是一个事务.如果数据库操作在某一步没有执行或出现异常而导致事务失败,这样有的事务被执行有的就没有被执行,从而就有了事务的回滚,取消先前 ...

  6. apache2.4以上版本配置虚拟主机

    一  将 主配置文件 httpd.conf中 #Include conf/extra/httpd-vhosts.conf 前面的# 去掉 二  进入conf/extra 修改 /conf/extra/ ...

  7. 用struts实现简单的登录

    1.建项目时选java EE6.0 2.写登陆界面 <body> <center> <form id="form1" name="form1 ...

  8. Entity framework在用于WCF时创建数据模型的问题

    众所周知,WCF的传输对象,在创建时需要在类名上标识[DataContract]以及在属性上标识[DataMember],当我们在使用Entity framework时(不考虑Code first的情 ...

  9. python——线程与多线程基础

    我们之前已经初步了解了进程.线程与协程的概念,现在就来看看python的线程.下面说的都是一个进程里的故事了,暂时忘记进程和协程,先来看一个进程中的线程和多线程.这篇博客将要讲一些单线程与多线程的基础 ...

  10. (49) odoo context操作

    * context  这是一个上下文,运用很灵活 * 得到整个context  V7  context=dict(context or {})  这个版本是明传  V8   self.context_ ...