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;
import cn.itcast.ssm.po.ItemsQueryVo;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ItemsMapperCustom {
//商品查询列表
public List findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
4.2、service层
4.2.1、service接口
package cn.itcast.ssm.service;
import java.util.List;
import cn.itcast.ssm.po.ItemsCustom;
import cn.itcast.ssm.po.ItemsQueryVo;
public interface ItemsService {
//商品查询列表
public List findTtemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
4.2.2、service接口实现类
还记得外层向里的调用逻辑吗?:Handler中可以调用service接口,service中可以调用mapper接口。
由于service要调用mapper,故需要在service接口实现类中注入mapper代理对象
package cn.itcast.ssm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcast.ssm.mapper.ItemsMapperCustom;
import cn.itcast.ssm.po.ItemsCustom;
import cn.itcast.ssm.po.ItemsQueryVo;
import cn.itcast.ssm.service.ItemsService;
@Service(“itemsService”)
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsMapperCustom itemsMapperCustom;
@Override
public List findTtemsList(ItemsQueryVo itemsQueryVo) throws Exception {
return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}
4.3、表现层
4.3.1、编写Controller(就是Handler)
由于表现层handler要调用service接口,故需要在handler中注入service接口。
package cn.itcast.ssm.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.itcast.ssm.po.Items;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?
掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
itcast.ssm.po.Items;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-zY8BcOej-1713332508123)]
[外链图片转存中…(img-LKgA9wbN-1713332508124)]
[外链图片转存中…(img-hCMKWpBb-1713332508124)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?
掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。
[外链图片转存中…(img-Qv0q5i2J-1713332508125)]
[外链图片转存中…(img-EBi0ESvJ-1713332508125)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!