搭建SSM框架
1.整合spring与mybatis
a>创建、编写配置文件: 包括applicationContext.xml mybatis.cfg.xml
applicationContext.xml:
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:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 13 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> 14 <!-- 配置数据源 --> 15 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 16 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 17 <property name="url" value="jdbc:mysql://localhost:3306/test"/> 18 <property name="username" value="root"/> 19 <property name="password" value="1234abcD"/> 20 </bean> 21 <!-- 声明式事务配置 开始 --> 22 <!-- 配置事务管理器 --> 23 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 24 <property name="dataSource" ref="dataSource"/> 25 </bean> 26 <!-- 配置事务通知 --> 27 <tx:advice id="txAdvice" transaction-manager="txManager"> 28 <tx:attributes> 29 <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 --> 30 <tx:method name="add" propagation="REQUIRED"/> 31 <tx:method name="insert" propagation="REQUIRED"/> 32 <tx:method name="update" propagation="REQUIRED"/> 33 <tx:method name="delete" propagation="REQUIRED"/> 34 <tx:method name="remove*" propagation="REQUIRED"/> 35 <tx:method name="get" read-only="true"/> 36 <tx:method name="*" propagation="REQUIRED"/> 37 </tx:attributes> 38 </tx:advice> 39 <aop:config> 40 <aop:pointcut expression="execution(* com.dcp.dao.*.*(..))" id="pointcut"/> 41 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> 42 </aop:config> 43 <!-- 声明式事务配置 结束 --> 44 <!-- 配置sqlSessionFactory --> 45 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 46 <property name="dataSource" ref="dataSource"/> 47 <property name="configLocation" value="classpath:mybatis.cfg.xml"></property> 48 </bean> 49 <!-- 自动扫描 所配置包下的所有注解 50 dao-@Repository("userDao") 51 service-@Service("userService") 52 action-@Controller("userAction") 53 属性的注入:@autowired 54 --> 55 56 <context:component-scan base-package="com.dcp"></context:component-scan> 57 58 <!-- <context:component-scan base-package="com.dcp"/> --> 59 <mybatis:scan base-package="com.dcp.dao" /> 60 61 </beans>
mybatis.cfg.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <typeAliases> 7 <package name="com.dcp.vo"/> 8 </typeAliases> 9 <mappers> 10 <package name="com.dcp.dao"/> 11 </mappers> 12 </configuration>
b>通过mybatis自动创建dao层 和 model代码,需要创建一个配置文件,右键pom.xml运行Run As 选择Maven build... 输入mybatis-generator:generate 运行之后刷新,就可以自动生成了
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- classPathEntry:数据库的JDBC驱动的jar包地址 --> <classPathEntry location="C:\Users\Administrator\Desktop\myLibs\mysql_driver\mysql-connector-java-5.1.34.jar" /> <context id="mysqlTables" defaultModelType="flat" targetRuntime="MyBatis3"> <property name="autoDelimitKeywords" value="true"/> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <property name="useActualColumnNames" value="false"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <commentGenerator> <!-- 抑制警告 --> <property name="suppressTypeWarnings" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="1234abcD"/> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.dcp.vo" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis.generator" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </sqlMapGenerator> <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.dcp.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaClientGenerator> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --> <table tableName="user"> <property name="useActualColumnNames" value="true"/> <columnOverride column="content" jdbcType="VARCHAR"/> </table> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --> <table tableName="class"> <property name="useActualColumnNames" value="true"/> <columnOverride column="content" jdbcType="VARCHAR"/> </table> </context> </generatorConfiguration>
2.整合springmvc
a>修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 指定另一个配置的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:app*.xml</param-value> </context-param> <filter> <filter-name>charEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 控制session开关的过滤器 --> <!-- <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <!-- 加载applicationContext.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC 注册和映射 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
b>配置springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" 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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven enable-matrix-variables="true"/> <!-- 指定自动扫描路径 --> <context:component-scan base-package="com.dcp.action"></context:component-scan> </beans>
3.编写控制器类
@Controller @RequestMapping(value="/dept") public class DeptAction { @Resource private DeptDaoImpl deptDao; @RequestMapping(value="/insert") public String insert(Dept dept){ System.out.println("---action.dept:"+dept); deptDao.insertDept(dept); return "forward:/jsp/main.jsp"; } }
缩写跳转页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> this is main jsp </body> </html>
测试ssm整合
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <form action="dept/insert.action" method="post"> 名称:<input type="text" name="deptName"><br> 地址:<input type="text" name="deptAddress"><br> <input type="submit" value="ok"> </form> </body> </html>
SSM:
Spring+SpringMVC+MyBaitis
也就是 IoC容器+MVC框架+ORM框架
IoC的意思是控制反转,意思是把创建和查找依赖对象的控制权交给容器而不是自己实例化对象;
MVC框架采用MVC分层,模型层处理数据逻辑,通常是模型对象在数据库存取数据,视图层处理数据显示,控制器层处理用户交互,通常从视图读取数据,控制用户输入,并向模型发送数据;
ORM框架即对象-关系映射模型,在数据库表和持久化对象间进行映射,这样在操作数据库时只需要对对象操作。
1.Spring
在IoC容器方面。Spring是轻量级的IoC和AOP容器。IoC容器是Spring的核心,负责创建对象,管理对象,装配对象,配置对象,并且管理这些对象的整个生命周期。管理对象动态向某个对象提供其他对象,通过依赖注入来实现,Spring有三种注入方式:接口注入、Set注入和构造注入。Spring AOP即面向切面编程,可以用在日志和事务管理等方面。
2.SpringMVC
SpringMVC是方法级别的拦截,一个方法对应一个request上下文
SpringMVC是单例的,只有一个实例,方法之间变量不共享
SpringMVC的核心控制器是Servlet
SpringMVC用的是独立的AOP方式
@ResponseBody,能自动将对象转换为JSON数据
3.MyBaitis
MyBatis的sql语句是手动编写的,可以进行更为细致的SQL优化,可以减少查询字段,具有高度灵活,可优化,易维护的特点。但需要维护SQL和结果映射,工作量大。