Servlet+Spring+Mybatis初试
1.导入相关的jar包
druid
mybatis
mybatis-spring
pageHelper
mysql驱动包
spring-context-support
spring-aspect
spring-jdbc 事务切面
spring-tx 事务建议
spring-web
servlet-api
jsp-api
jstl
standard
lombok
log4j
2.相关的配置文件
jdbc.propertites
applicationContext.xml
*mapper.xml
log4j.properties
3.记住一些核心类
DruidDataSource 德鲁伊的核心类
SqlSessionFactoryBean mybatis-spring变为mybatis-config.xml
DataSourceTransactionManager 事务切面
做建议 read-only
4.步骤
1.先导入jar包
2.编写核心配置文件
2.1 数据源处理
2.2 SqlSessionFactoryBean
2.3 SqlSessionTemplate
2.4 切面 DataSourceTransactionManager
2.5 AOP 关注点 execution(* com.blb.service..*(..)) 关注的都是业务层,要么都执行,要么都不执行
2.6 为当前事务设置一些建议,哪些方法要事务,哪些方法不要事务
增删改 一定要事务
查询 可加可不加
2.7 构建项目
分层设计 遵循三层的设计原则 BeanUtils用于进行多层实体类之间的数据转换
控制层 controller/action vo
业务层 biz/service bo
持久层 dao/repository
实体类 domain/pojo/dto/model/bean
3.具体实现
3.1 编写映射文件 SQL语句 mybatis讲究的是代码和SQL分离
3.2 在DAO中注入sqlSession
3.3 编写持久层代码
3.4 编写业务层代码
3.5 编写控制层代码
4.和我们的web项目做一个整合
我们必须引入spring-web的jar包 主要的用途是用来当前项目初始化就加载配置文件并生成IOC容器的
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-4.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 16 "> 17 18 <!--启用注解--> 19 <context:annotation-config></context:annotation-config> 20 <!--引入外部的属性文件jdbc.properties--> 21 <context:property-placeholder location="classpath*:jdbc.properties"></context:property-placeholder> 22 <!--配置扫描路径--> 23 <context:component-scan base-package="com.blb"></context:component-scan> 24 <!--将druid连接池加入到IOC容器中--> 25 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 26 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 27 <property name="url" value="jdbc:mysql://localhost:3306/rbac2?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8"></property> 28 <property name="username" value="root"></property> 29 <property name="password" value="root"></property> 30 31 </bean> 32 33 <!--分页插件--> 34 <bean id="pageHelper" class="com.github.pagehelper.PageInterceptor"> 35 <property name="properties"> 36 <props> 37 <prop key="helperDialect">mysql</prop> 38 </props> 39 </property> 40 </bean> 41 42 43 <!--sqlSessionFactoryBean加入到容器中--> 44 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> 45 <property name="dataSource" ref="dataSource"></property> 46 <property name="plugins"> 47 <array> 48 <ref bean="pageHelper"></ref> 49 </array> 50 </property> 51 <property name="typeAliasesPackage" value="com.blb.dto"></property> 52 <property name="mapperLocations" value="classpath*:*com/blb/mapper/*.xml"></property> 53 </bean> 54 <!--sqlSessionTemplate--> 55 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 56 <constructor-arg index="0" ref="sqlSessionFactoryBean"></constructor-arg> 57 </bean> 58 <!--配置jdbc包中切面--> 59 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 60 <property name="dataSource" ref="dataSource"></property> 61 </bean> 62 <!--配置建议规则--> 63 <tx:advice id="tx" transaction-manager="transactionManager"> 64 <tx:attributes> 65 <tx:method name="get*" read-only="true"/> 66 <tx:method name="select*" read-only="true"></tx:method> 67 <tx:method name="query*" read-only="true"></tx:method> 68 <tx:method name="insert*" propagation="REQUIRED"></tx:method> 69 <tx:method name="save*" propagation="REQUIRED"></tx:method> 70 <tx:method name="update*" propagation="REQUIRED"></tx:method> 71 <tx:method name="modify*" propagation="REQUIRED"></tx:method> 72 <tx:method name="remove*" propagation="REQUIRED"></tx:method> 73 <tx:method name="delete*" propagation="REQUIRED"></tx:method> 74 </tx:attributes> 75 </tx:advice> 76 77 <!--开启切面代理--> 78 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 79 <!--先来实现关注点--> 80 <aop:config> 81 <aop:pointcut id="service" expression="execution(* com.blb.service..*(..))"/> 82 <aop:advisor advice-ref="tx" pointcut-ref="service"></aop:advisor> 83 </aop:config> 84 85 </beans>
1 @Log4j 2 @WebServlet("/user") 3 public class UserController extends HttpServlet { 4 5 private UserService userService; 6 7 8 @Override 9 public void init() throws ServletException { 10 WebApplicationContext webcontainer = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 11 this.userService= (UserService)webcontainer.getBean("userServiceImpl"); 12 //与IOC容器交互,从IOC容器里拿到userService单列 13 } 14 15 16 @Override 17 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 String m = req.getParameter("m"); 19 if(m.equals("save")) 20 { 21 save(req,resp); 22 } 23 else if(m.equals("update")) 24 { 25 update(req,resp); 26 } 27 else if(m.equals("delete")) 28 { 29 delete(req,resp); 30 } 31 else if(m.equals("select")) 32 { 33 select(req,resp); 34 } 35 else if(m.equals("selectAll")) 36 { 37 selectAll(req,resp); 38 } 39 40 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 6 version="3.1"> 7 <!--1.告诉spring的核心配置文件叫什么名字--> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath*:applicationContext.xml</param-value> 11 </context-param> 12 <!--2.我给你一个监听器 监听器用来使用上面的配置,加载配置文件--> 13 <listener> 14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 15 </listener> 16 </web-app>