第一步:整合dao层
mybatis和spring整合,通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
第二步:整合service层
通过spring管理 service接口。使用注解或配置方式将service接口配置在spring配置文件中。
实现事务控制
第三步:整合springmvc
由于springmvc是spring的模块,不需要整合。
三、三层整合
======
3.1、整合dao层
3.1.1、mybatis配置文件
在mybatis自己的配置文件sqlMapConfig.xml中实现别名定义、缓存设置等配置。
<?xml version="1.0" encoding="UTF-8" ?>3.1.2、dao层spring配置文件applicationContext-dao.xml
在applicationContext-dao.xml中完成数据源、SqlSessionFactory、mapper扫描器的配置。
<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/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:property-placeholder location=“classpath:db.properties” />
<bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”
destroy-method=“close”>
3.2、整合service层
3.2.1、service层spring配置文件applicationContext-service.xml
在applicationContext-service.xml中管理service接口实现类的bean
<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/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:component-scan base-package=“cn.itcast.ssm.service.impl”></context:component-scan>
3.2.2、spring管理事务的配置文件applicationContext-transaction.xml
<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/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean id=“transactionManager”
class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“save*” propagation=“REQUIRED”/>
<tx:method name=“delete*” propagation=“REQUIRED”/>
<tx:method name=“insert*” propagation=“REQUIRED”/>
<tx:method name=“update*” propagation=“REQUIRED”/>
<tx:method name=“find*” propagation=“SUPPORTS” read-only=“true”/>
<tx:method name=“get*” propagation=“SUPPORTS” read-only=“true”/>
<tx:method name=“select*” propagation=“SUPPORTS” read-only=“true”/>
</tx:attributes>
</tx:advice>
aop:config
<aop:advisor advice-ref=“txAdvice” pointcut="execution(*
cn.itcast.ssm.service.impl..(…))"/>
</aop:config>
3.3、配置springmvc的配置文件
springmvc是spring的一个模块,不需要整合,但也需要在springmvc.xml中配置Handler、处理器映射器、适配器、视图解析器。
<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/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:component-scan base-package=“cn.itcast.ssm.controller”></context:component-scan>
mvc:annotation-driven</mvc:annotation-driven>
<bean
class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>
3.4、配置web.xml文件
在web.xml文件中完成一下几件事:
1、配置springmvc的前端控制器(加载springmvc配置文件)
2、加载spring的配置文件
3、加载监听器
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns=“http://xmlns.jcp.org/xml/ns/javaee” xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” id=“WebApp_ID” version=“3.1”>
springmvcfirst
contextConfigLocation
/WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
*.action
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
3.5、三层整合配置文件总结
3.5.1、dao层
1、mybatis自己的配置文件sqlMapConfig.xml
实现别名定义、缓存设置等配置。
2、dao层spring配置文件applicationContext-dao.xml
完成数据源、SqlSessionFactory、mapper扫描器的配置。
3.5.2、service层
1、service层spring配置文件applicationContext-service.xml
管理service接口实现类的bean(注解方式)
2、spring管理事务的配置文件applicationContext-transaction.xml
实现事务控制、AOP
3.5.3、表现层配置文件springmvc.xml
配置Handler、处理器映射器、适配器、视图解析器。
3.5.4、web.xml文件
配置springmvc的前端控制器(加载springmvc配置文件)、加载spring的配置文件、加载监听器
四、三层代码编写
========
4.1、dao层
4.1.1、逆向工程生成单表的po类及mapper
参照博客:MyBatis详解:逆向工程自动生成代码
4.1.2、手动定义多表的po类和mapper
针对综合查询mapper,一般情况会有关联查询,建议自定义po类和mapper,一般多表的po类继承自主表,次表以属性形式加载在po类中。
po类:ItemsCustom.java
这里仍是单表,多表的话在其中增加扩展属性即可。
package cn.itcast.ssm.po;
public class ItemsCustom extends Items {
//添加商品信息的扩展属性
}
ItemsQueryVo.java
package cn.itcast.ssm.po;
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统 可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
mapper.xml
在其中定义操作数据库的sql语句
<?xml version="1.0" encoding="UTF-8" ?>items.name LIKE ‘%${itemsCustom.name}%’
<select id=“findItemsList” parameterType=“cn.itcast.ssm.po.ItemsQueryVo”
resultType=“cn.itcast.ssm.po.ItemsCustom”>
SELECT items.* FROM items
mapper.java
在其中定义对应sql语句的抽象方法
package cn.itcast.ssm.mapper;
import cn.itcast.ssm.po.Items;
import cn.itcast.ssm.po.ItemsCustom;
import cn.itcast.ssm.po.ItemsExample;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
写在最后
作为一名即将求职的程序员,面对一个可能跟近些年非常不同的 2019 年,你的就业机会和风口会出现在哪里?在这种新环境下,工作应该选择大厂还是小公司?已有几年工作经验的老兵,又应该如何保持和提升自身竞争力,转被动为主动?
就目前大环境来看,跳槽成功的难度比往年高很多。一个明显的感受:今年的面试,无论一面还是二面,都很考验Java程序员的技术功底。
最近我整理了一份复习用的面试题及面试高频的考点题及技术点梳理成一份“Java经典面试问题(含答案解析).pdf和一份网上搜集的“Java程序员面试笔试真题库.pdf”(实际上比预期多花了不少精力),包含分布式架构、高可扩展、高性能、高并发、Jvm性能调优、Spring,MyBatis,Nginx源码分析,Redis,ActiveMQ、Mycat、Netty、Kafka、Mysql、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多个知识点高级进阶干货!
由于篇幅有限,为了方便大家观看,这里以图片的形式给大家展示部分的目录和答案截图!
Java经典面试问题(含答案解析)
阿里巴巴技术笔试心得
9xE3d-1712112625621)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-5WJQnOLy-1712112625621)]
写在最后
作为一名即将求职的程序员,面对一个可能跟近些年非常不同的 2019 年,你的就业机会和风口会出现在哪里?在这种新环境下,工作应该选择大厂还是小公司?已有几年工作经验的老兵,又应该如何保持和提升自身竞争力,转被动为主动?
就目前大环境来看,跳槽成功的难度比往年高很多。一个明显的感受:今年的面试,无论一面还是二面,都很考验Java程序员的技术功底。
最近我整理了一份复习用的面试题及面试高频的考点题及技术点梳理成一份“Java经典面试问题(含答案解析).pdf和一份网上搜集的“Java程序员面试笔试真题库.pdf”(实际上比预期多花了不少精力),包含分布式架构、高可扩展、高性能、高并发、Jvm性能调优、Spring,MyBatis,Nginx源码分析,Redis,ActiveMQ、Mycat、Netty、Kafka、Mysql、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多个知识点高级进阶干货!
由于篇幅有限,为了方便大家观看,这里以图片的形式给大家展示部分的目录和答案截图!
[外链图片转存中…(img-6LzDKj5W-1712112625621)]
Java经典面试问题(含答案解析)
[外链图片转存中…(img-GYUt1z3v-1712112625622)]
阿里巴巴技术笔试心得
[外链图片转存中…(img-QhETZ1zd-1712112625622)]