亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

SSM的整合

標簽:
Html/CSS

0 配置文件

Log4j.properties—》日志配置文件

Db.properties----》连接数据库,配置连接数据库的参数

Mybatis/SqlMapConfig.xml---》 mybatis的核心配置文件,Mybatis配置项

Spring/applicationContext.xml  -spring的核心配置文件
配置公用的内容:数据源、事务管理

Spring/applicationContext- base-dao.xml   配置dao
配置SqlSessionFactory,dao(mapper)

Spring/applicationContext-base-service.xml -配置service
配置业务接口

Spring/Springmvc.xml---》配置springmvc
处理器映射器
处理器适配器
视图解析器
拦截器
….

配置文件目录结构如下:


webp

db.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:yycg  #最后为数据库名称jdbc.username=yycg
jdbc.password=yycg
jdbc.maxActive=5jdbc.maxIdle=2
Log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
applicationContext.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.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "><!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
       <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="${jdbc.maxActive}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/></bean><!-- 事务管理器 --><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" rollback-for="java.lang.Exception"/>
    <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
    <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
    <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
    <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
    <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
  </tx:attributes></tx:advice><!-- 切面 --><aop:config proxy-target-class="true">
  <aop:advisor advice-ref="txAdvice"
  pointcut="execution(* yycg.*.service.impl.*.*(..))"/></aop:config></beans>
Springmvc.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.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

    <!-- 组件扫描 扫描所有标记@Controller类,由于使用自动扫描所以action类不用在spring配置文件中配置 -->
    <context:component-scan base-package="yycg.**.action" />

    <!-- 处理器映射器和适配器,可以使用mvc注解驱动 -->
    <mvc:annotation-driven/>


    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 将jstl的jar包加入工程,默认支持jstl -->
        <!-- 前缀和后缀可以去掉的,为了方便开发才加的 -->
        <property name="prefix" value="/WEB-INF/jsp" />
        <property name="suffix" value=".jsp" />
    </bean>

    </beans>
applicationContext- base-dao.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.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "><!-- 配置SqlSessionFactory
从spring和mybatis的整合包中获取
 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 加载数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 配置SqlMapConfig.xml -->
    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/></bean><!-- 使用MapperFactoryBean 生成mapper的代理对象
在mybatis和spring的整合包中
--><!-- <bean id="sysuserCustomMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  配置mapper接口
  <property name="mapperInterface" value="yycg.base.dao.mapper.SysuserCustomMapper"/>
  配置sqlSessionFactory
  <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> --><!--配置 mapper自动扫描器
 bean名称就是mapper类型(首字母小写)
 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <!-- 配置扫描包路径 ,如果扫描多个包路径,中间使用半角逗号分隔-->
   <property name="basePackage" value="yycg.base.dao.mapper"/>
    <!-- 配置SqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean></beans>

1  Dao整合

mybatis逆向工程
根据数据库表结构生成mapper.xml和mapper.java及po类。生成的mapper具备了单表的增、删、改、查的功能。

在 generatorConfig.xml配置生成规则 ,执行GeneratorSqlmap.java

<?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>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    <!--    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
            password="mysql">
        </jdbcConnection> -->
        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
            userId="yycg"
            password="yycg">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="yycg.base.pojo.po"
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="yycg.base.dao.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="yycg.base.dao.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table schema="" tableName="sysuser"></table>
        
        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context></generatorConfiguration>
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("generatorConfig-base.xml"); 
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);

逆向工程生成的mapper.xml相关方法测试

public class SysuserMapperTest extends TestCase {    private ApplicationContext applicationContext;    
    private SysuserMapper sysuserMapper;    protected void setUp() throws Exception {        // 获取spring容器
        applicationContext = new ClassPathXmlApplicationContext(new String[] {                "spring/applicationContext.xml", "spring/applicationContext-dao.xml"

        });
        sysuserMapper = (SysuserMapper) applicationContext.getBean("sysuserMapper");
    }    protected void tearDown() throws Exception {        super.tearDown();
    }    //根据主键删除
    public void testDeleteByPrimaryKey() {
        sysuserMapper.deleteByPrimaryKey("test009");
    }    //插入用户信息
    public void testInsert() {
        
        Sysuser sysuser = new Sysuser();
        sysuser.setId(UUIDBuild.getUUID());//生成uuid主键
        
        sysuser.setUserid("");
        sysuser.setUsername("");
        sysuserMapper.insert(sysuser);
    }    //自定义查询条件查询
    public void testSelectByExample() {
        SysuserExample sysuserExample  =new SysuserExample();
        SysuserExample.Criteria criteria = sysuserExample.createCriteria();        //自定义查询条件
        criteria.andUsernameEqualTo("test009");
        criteria.andGroupidEqualTo("4");
        
        List<Sysuser> list = sysuserMapper.selectByExample(sysuserExample);
        
        System.out.println(list.get(0));
    }    //根据主键查询用户信息
    public void testSelectByPrimaryKey() {
        Sysuser sysuser =  sysuserMapper.selectByPrimaryKey("18e6c4bc2f3244bfa146ec6e3f73919a");
        System.out.println(sysuser);
    }    //根据主键更新,传入的po类的属性不为空才更新
    public void testUpdateByPrimaryKeySelective() {        //定义一个新的对象
        Sysuser sysuser = new Sysuser();        //对象设置id
        sysuser.setId("18e6c4bc2f3244bfa146ec6e3f73919a");        //再设置要更新的值
        sysuser.setUsername("test009999");
        sysuserMapper.updateByPrimaryKeySelective(sysuser);
    }    //根据主键更新,不管传入的po类的属性是否为空,都更新
    public void testUpdateByPrimaryKey() {        
        //先查询对象
        Sysuser sysuser = sysuserMapper.selectByPrimaryKey("18e6c4bc2f3244bfa146ec6e3f73919a");        //向对象中设置要更新的值
        sysuser.setUsername("test009999");        //执行更新
        sysuserMapper.updateByPrimaryKey(sysuser);
    }

}

2 service 整合

让spring统一管理service接口,在service中调用mapper。在service层实现事务控制,使用声明式事务配置方法

编写service接口和实现 :


webp

在applicationContext-service.xml配置:

webp

3 action整合

整合目标:在action中调用service接口,从service中获取数据传到页面。
实现系统首页(使用jquery Easyui)。

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>yycgproject</display-name>

    <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 解决post乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <!-- 屏蔽springmvc自动注册的异常处理器 -->
        <!-- <init-param>
            <param-name>detectAllHandlerExceptionResolvers</param-name>
            <param-value>false</param-value>
        </init-param> -->

        <!-- <load-on-startup>1</load-on-startup> -->
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!-- dwr的servlet配置 -->
    <!-- <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
        是否允许调试,如果要在浏览器中调试则必须设置为true
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
        如果允许跨域请求,则必须将此值设置为false,默认值为true
        <init-param>
            <param-name>crossDomainSessionSecurity</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>allowScriptTagRemoting</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet> -->
    <!-- <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping> -->

    <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></web-app>

action代码

package yycg.base.action;@Controllerpublic class FirstAction {    
    @Autowired
    private UserService userService;    
    //首页
    @RequestMapping("/first")    public String first(Model model)throws Exception{
        
        Sysuser sysuser =  userService.findSysuserById("189");        
        //将sysuser传页面
        model.addAttribute("sysuser", sysuser);        
        return "/base/first";
    }    
    //欢迎页面
    @RequestMapping("/welcome")    public String welcome(){        
        return "/base/welcome";
    }

}



作者:峰加
链接:https://www.jianshu.com/p/202cbd03e165


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
233
獲贊與收藏
1006

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消