潍坊网站排名提升,学校手机网站模板,网站建设合同模板91075,自己做公众号和小说网站推广文章目录 SSM(Vue3ElementPlusAxiosSSM前后端分离)--基础环境搭建【三】项目介绍项目功能/界面● SSM 整合项目界面 配置Spring 和MyBatis , 并完成整合 SSM(Vue3ElementPlusAxiosSSM前后端分离)–基础环境搭建【三】
项目介绍
项目功能/界面
● SSM 整合项目界面 配置Sprin… 文章目录 SSM(Vue3ElementPlusAxiosSSM前后端分离)--基础环境搭建【三】项目介绍项目功能/界面● SSM 整合项目界面 配置Spring 和MyBatis , 并完成整合 SSM(Vue3ElementPlusAxiosSSM前后端分离)–基础环境搭建【三】
项目介绍
项目功能/界面
● SSM 整合项目界面 配置Spring 和MyBatis , 并完成整合
1、创建spring 的配置文件applicationContext.xml : 主要配置和业务逻辑有关的比如数据源事务控制等 2、创建furns_ssm\src\main\resources\applicationContext.xml 并加入必要的命名空间, 提示同样适用前面的方式创建: 右键-New-XML configuration -Spring Config
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aop xmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd!-- spring的配置文件 : 主要配置和业务逻辑有关的比如数据源事务控制等 --!--配置扫描com.nlc 包但是不扫描控制器 控制器由springmvc 管理1. 扫描com.nlc.furn包 [包括子包]2. context:exclude-filter 配置说明 不扫描控制器--context:component-scan base-packagecom.nlc.furncontext:exclude-filter typeannotationexpressionorg.springframework.stereotype.Controller//context:component-scan!--引入外部的jdbc.properties文件--context:property-placeholder locationclasspath:jdbc.properties/!--配置数据源对象-DataSoruce Druid数据源--bean classcom.alibaba.druid.pool.DruidDataSource idpooledDataSource!--给数据源对象配置属性值--property nameusername value${jdbc.user}/property namepassword value${jdbc.pwd}/property namedriverClassName value${jdbc.driver}/property nameurl value${jdbc.url}//bean!--配置mybatis和spring整合1、在项目中引入 mybatis整合到spring的适配库/包2. 这里爆红是因为你还没有相应的文件, 当有文件时就不会爆红--bean classorg.mybatis.spring.SqlSessionFactoryBean idsqlSessionFactory!--指定mybatis全局配置文件--property nameconfigLocation valueclasspath:mybatis-config.xml/!--指定数据源--property namedataSource refpooledDataSource/!--指定mybatis的mapper文件[Mapper.XML]位置1、我们在开发中, 通常将mapper.xml放在类路径 resources/mapper2. 所以这里指定的value 是 classpath:mapper/*.xml--property namemapperLocations valueclasspath:mapper/*.xml//bean!-- 配置扫描器将mybatis接口的实现加入到ioc容器中1、我们的mapper接口放在com.nlc.furn.dao2. mybatis就是处于DAO层, 操作DB--bean classorg.mybatis.spring.mapper.MapperScannerConfigurer!--1. 扫描所有的dao接口的实现加入到ioc容器中2. 这里dao接口就是mapper接口--property namebasePackage valuecom.nlc.furn.dao//bean!--配置事务管理器-对象1. DataSourceTransactionManager 这个对象是进行事务管理2. 一定要配置数据源属性这样指定该事务管理器 是对哪个数据源进行事务控制--bean classorg.springframework.jdbc.datasource.DataSourceTransactionManager idtransactionManagerproperty namedataSource refpooledDataSource//bean!--lt;!ndash;配置启动基于注解的声明式事务管理功能--!--这是以前的玩法, 使用XML配置切入表达式来玩--!--ndash;gt;--!--tx:annotation-driven transaction-managertransactionManager/--!--解读1. 开启基于注解的事务并指定切入点2. execution(* com.nlcfurns.service..*(..)):表示对com.nlc.furns.service包所有类的所有方法控制事务3. tx:advice : 配置事务增强, 也就是指定事务如何切入4. 不需要背但是能看到能修改能维护--aop:config!-- 切入点表达式 --aop:pointcut idtxPoint expressionexecution(* com.nlc.furn.service..*(..))/!-- 配置事务增强/规则: 使用txAdvice 指定规则对 txPoint进行切入--aop:advisor advice-reftxAdvice pointcut-reftxPoint//aop:config!-- 配置事务增强【指定事务规则】也就是指定事务如何切入--tx:advice idtxAdvicetx:attributes!-- *代表所有方法都是事务方法--tx:method name*/!-- 以get开始的所有方法 我们认为是只读进行调优--tx:method nameget* read-onlytrue//tx:attributes/tx:advice/beans3.创建furns_ssm\src\main\resources\jdbc.properties , 配置连接mysql 的信息
jdbc.userNameroot
jdbc.password123456
jdbc.driverClasscom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/furns_ssm?userSSLtrueamp;userUnicodetrueamp;characterEncodingUTF-8
#密码和端口是自己的4.在类路径resources 下创建furns_ssm\src\main\resources\mybatis-config.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configuration
PUBLIC -//mybatis.org//DTD Config 3.0//EN
http://mybatis.org/dtd/mybatis-3-config.dtd
configuration
/configuration5.在类路径下创建mapper 目录存放mapper 的xml 文件 6.完成测试: 创建furns-ssm\src\test\java\com\nlc\furns\test\T1.java
public class T1 {Testpublic void t1() {//看看spring配置的Bean是否可以获取到//1. 获取到容器ApplicationContext ioc new ClassPathXmlApplicationContext(applicationContext.xml);//获取BeanSystem.out.println(ioc.getBean(pooledDataSource));System.out.println(ioc.getBean(sqlSessionFactory));}
}看看是否能够得到Spring 容器的数据源对象和会话工厂对象.