A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是楠楠 于 2018-4-10 15:27 编辑


【郑州校区】springmvc-sublun-day01(中)

1 整合mybatis
        为了更好的学习springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合。
整合目标:控制层采用用SpringMVC,持久层使用MyBatis的实现。
1.1 需求
实现商品查询列表,从MySQL的数据库查询商品信息。
1.2 罐包
包括:弹簧(包括用SpringMVC),MyBatis的,MyBatis的弹簧整合包,数据库驱动,第三方连接池。
参考:“MyBatis的与用SpringMVC整合全部的jar包”目录
1.3 工程搭建 1.3.1 整合思路
道层:
1,SqlMapConfig.xml,空文件即可。需要文件头。
2,applicationContext-dao.xml。
a)数据库连接池
b)SqlSessionFactory对象,需要spring和mybatis整合包下的。
c)配置映射文件扫描器。
服务层:
1,applicationContext-service.xml包扫描器,扫描@service注解的类。
2,applicationContext-trans.xml配置事务。
表现层:
Springmvc.xml
1,包扫描器,扫描@Controller注解的类。
2,配置注解驱动。
3,视图解析器
在web.xml
配置前端控制器。
1.3.2 sqlMapConfig.xml
在classpath下创建mybatis / s qlMapConfig.xml
<?xml version = “1.0” encoding = “UTF-8”?>
<!DOCTYPE 配置
PUBLIC “ -  // mybatis.org //DTD Config 3.0 // EN”
< 配置>
</ configuration >
1.3.3 applicationContext-dao.xml
配置数据源,配置SqlSessionFactory中,映射扫描器。
<?xml version = “1.0” encoding = “UTF-8”?>
        xmlns:context = http://www.springframework.org/schema/context xmlns:p = http://www.springframework.org/schema/p
        xmlns:aop = http://www.springframework.org/schema/aop xmlns:tx = http://www.springframework.org/schema/tx
        xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd >
        <! - 加载配置文件 - >
        < context:property-placeholder location = “classpath:db.properties” />
        <! - 数据库连接池 - >
        < bean id = “dataSource” class = “org.apache.commons.dbcp.BasicDataSource”
                destroy-method = “close” >
                < property name = “driverClassName” value = “$ {jdbc.driver}” />
                < property name = “url” value = “$ {jdbc.url}” />
                < property name = “username” value = “$ {jdbc.username}” />
                < property name = “password” value = “$ {jdbc.password}” />
                < property name = “maxActive” value = “10” />
                < property name = “maxIdle” value = “5” />
        </ bean >
        <! - mapper配置 - >
        <! -让弹簧管理SqlSessionFactory中使用的MyBatis和弹簧整合包中的- >
        < bean id = “sqlSessionFactory” class = “org.mybatis.spring.SqlSessionFactoryBean” >
                <! - 数据库连接池 - >
                < property name = “dataSource” ref = “dataSource” />
                <! -加载的MyBatis的全局配置文件- >
                < property name = “configLocation” value = “classpath:mybatis / SqlMapConfig.xml” />
        </ bean >
        <! - 配置Mapper扫描器 - >
        < bean class = “org.mybatis.spring.mapper.MapperScannerConfigurer” >
                < property name = “basePackage” value = “cn.itcast.springmvc.mapper” />
        </ bean >
</ beans >
Db.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:// localhost:3306 / springmvc ?characterEncoding = utf-8
jdbc.username = root
jdbc.password = root
1.3.4 applicationContext-service.xml
<?xml version = “1.0” encoding = “UTF-8”?>
        xmlns:context = http://www.springframework.org/schema/context xmlns:p = http://www.springframework.org/schema/p
        xmlns:aop = http://www.springframework.org/schema/aop xmlns:tx = http://www.springframework.org/schema/tx
        xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd >
        < context:component-scan base-package = “cn.itcast.springmvc.service” />
</ beans >
1.3.5 applicationContext-transaction.xml
<?xml version = “1.0” encoding = “UTF-8”?>
        xmlns:context = http://www.springframework.org/schema/context xmlns:p = http://www.springframework.org/schema/p
        xmlns:aop = http://www.springframework.org/schema/aop xmlns:tx = http://www.springframework.org/schema/tx
        xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd >
        <! - 事务管理器 - >
        < bean id = “transactionManager”
                class = “org.springframework.jdbc.datasource.DataSourceTransactionManager” >
                <! - 数据源 - >
                < property name = “dataSource” ref = “dataSource” />
        </ bean >
        <! - 通知 - >
        < tx:advice id = “txAdvice” transaction-manager = “transactionManager” >
                < tx:attributes >
                        <! - 传播行为 - >
                        < tx:method name = “save *” propagation = “REQUIRED” />
                        < tx:method name = “insert *” propagation = “REQUIRED” />
                        < tx:method name = “delete *” propagation = “REQUIRED” />
                        < tx:method name = “update *” propagation = “REQUIRED” />
                        < tx:method name = “find *” propagation = “SUPPORTS”只读= “true” />
                        < tx:method name = “get *” propagation = “SUPPORTS”只读= “true” />
                </ tx:attributes >
        </ tx:advice >
        <! - 切面 - >
        < aop:config >
                < aop:advisor advice-ref = “txAdvice”
                        pointcut = “执行(* cn.itcast.springmvc.service。*。*(..))” />
        </ aop:config >
</ beans >
1.3.6 springmvc.xml
<?xml version = “1.0” encoding = “UTF-8”?>
        xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xmlns:p = http://www.springframework.org/schema/p
        xmlns:context = http://www.springframework.org/schema/context
        xmlns:dubbo = http://code.alibabatech.com/schema/dubbo xmlns:mvc = http://www.springframework.org/schema/mvc
        <! - 扫描带控制器注解的类 - >
        < context:component-scan base-package = “cn.itcast.springmvc.controller” />
        <! - 加载注解驱动 - >
        < mvc:annotation-driven />
        <! - 视图解析器 - >
        < bean class = “org.springframework.web.servlet.view.InternalResourceViewResolver” >
                < property name = “viewClass”
                        = “org.springframework.web.servlet.view.JstlView” />
                <! - jsp前缀 - >
                < property name = “prefix” value = “/ WEB-INF / jsp /” />
                <! - jsp后缀 - >
                < property name = “suffix” value = “.jsp” />
        </ bean >
</ beans >
1.3.7 web.xml
<?xml version = “1.0” encoding = “UTF-8”?>
< web-app xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
        xmlns = http://java.sun.com/xml/ns/javaee
        id = “WebApp_ID” version = “2.5” >
        < display-name > springmvc -web </ display-name >
        < welcome-file-list >
                < welcome-file > index.html </ welcome-file >
                < welcome-file > index.htm </ welcome-file >
                < welcome-file > index.jsp </ welcome-file >
                < welcome-file > default.html </ welcome-file >
                < welcome-file > default.htm </ welcome-file >
                < welcome-file > default.jsp </ welcome-file >
        </ welcome-file-list >
        <! - 加载spring容器 - >
        < context-param >
                < param-name > contextConfigLocation </ param-name >
                < param-value > classpath:spring / applicationContext- *。xml </ param-value >
        </ context-param >
        < listener >
                < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
        </ listener >
        
        < servlet >
                < servlet-name > springmvc </ servlet-name >
                < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
                < init-param >
                        < param-name > contextConfigLocation </ param-name >
                        < param-value > classpath:spring / springmvc.xml </ param-value >
                </ init-param >
        </ servlet >
        < servlet-mapping >
                < servlet-name > springmvc </ servlet-name >
                < url-pattern > * .action </ url-pattern >
        </ servlet-mapping >
</ web-app >
1.4
MyBatis的逆向工程。
1.5 服务
如图1所示,服务由弹簧管理
2,弹簧对服务进行事务控制。
1.5.1 ItemService接口
公共接口 ItemService {
        List <Items> getItemsList();
}
1.5.2 ItemServiceImpl实现类
@服务
公共 ItemServiceImpl 实现 ItemService {
        @ Autowired
        私人 ItemMapper itemMapper ;
        
        @覆盖
        public List <Items> getItemsList(){
                List <Items> itemList = itemMapper .getItemList();
                返回 itemList ;
        }
}
1.6 控制器
@Controller
公共 ItemController {
        @Autowired
        private ItemService itemService ;
        
        @RequestMapping “/ itemList”
        public ModelAndView getItemList (){
                List <Items> list = itemService .getItemsList();
                ModelAndView modelAndView = new ModelAndView();
                modelAndView .addObject(“itemList” list );
                modelAndView .setViewName(“itemList” );
                返回 modelAndView ;
        }
}
1.7 测试
传智播客·黑马程序员郑州校区地址
河南省郑州市高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话0371-56061160 / 61/62
来校路线地铁一号线梧桐街站A口出


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马