ssm

作者: 奋斗的磊哥 | 来源:发表于2017-10-25 19:41 被阅读0次

    工程目录结构

    image.png

    mybatis逆向工程

    逆向工程配置文件

    generatorConfig.xml文件

    <?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>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
    
            <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
            <jdbcConnection driverClass="驱动类"
                connectionURL="连接地址" userId="用户名"
                password="密码">
            </jdbcConnection>
    
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
                和 NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- targetProject:生成POJO类的位置 -->
            <javaModelGenerator targetPackage="生成POJO类的位置"
                targetProject=".\src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!-- targetProject:mapper映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="生成mapper映射文件的位置"
                targetProject=".\src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
    
            <!-- targetPackage:mapper接口生成的位置 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="mapper接口生成的位置" targetProject=".\src">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
    
            <!-- 指定数据库表 -->
            <!-- tableName:表名
            domainObjectName:生成java对象的类名
             -->
            <table tableName="user" 
                    domainObjectName="User"
                    enableUpdateByExample="false"
                    enableDeleteByExample="false">
            </table>
            <table tableName="app_version" 
                    domainObjectName="AppVersion"
                    enableUpdateByExample="false"
                    enableDeleteByExample="false">
            </table>
            <table tableName="app_info" 
                    domainObjectName="AppInfo"
                    enableUpdateByExample="false"
                    enableDeleteByExample="false">
            </table>
            <table tableName="app_category" 
                    domainObjectName="AppCategory"
                    enableUpdateByExample="false"
                    enableDeleteByExample="false">
            </table>
        </context>
    </generatorConfiguration>
    

    逆向工程代码

    public class GeneratorSqlmap {
        public void generator() throws Exception {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            // 指定逆向工程配置文件
            File configFile = new File("generatorConfig.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);
        }
    
        public static void main(String[] args) throws Exception {
            try {
                GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
                generatorSqlmap.generator();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    测试类(可以在ssm整合的时候测试)

    public static void main(String[] args) throws IOException {
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
            SqlSession session = factory.openSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
            UserExample example=new UserExample();
            example.createCriteria().andUserNameEqualTo("dev2").andUserPwdEqualTo("123");
            List<User> list = mapper.selectByExample(example);
            System.out.println(list);
        }
    

    spring-mybatis整合

    1、applicationContext-mybatis.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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <context:property-placeholder location="classpath:properties/db.properties"/>      
        <!-- 配置数据 -->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${jdbc.driver}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>     
        <!-- 配置SqlSessionFactoryBean:用来创建SqlSessionFacotry,并且扫描xml文件-->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFacotry">
            <!--注入数据源-->
            <property name="dataSource" ref="dataSource"></property>
            <!--配置别名,如果在下面的mybatis配置文件中有配置别名,那么这句可以不用-->
            <!-- <property name="typeAliasesPackage" value="com.hemi.bean"></property> -->
            <!--加载mybatis配置文件-->
            <property name="configLocation" value="classpath:spring/mybatis-config.xml"></property>
            <!-- 扫描mapper.xml -->
            <property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
        </bean>
        <!-- 配置MapperScannerConfigurer:用来扫描mapper接口 --> 
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 扫描mapper接口 -->
            <property name="basePackage" value="com.hemi.dao"/>
            <!--注入sqlSessionFacotry-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property>
        </bean>
    </beans>
    

    2、mybatis-config.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
     <configuration>
       <settings>
           <setting name="autoMappingBehavior" value="FULL"/>          
       </settings>
     </configuration>
    

    3、db.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/appmanager?useUnicode=true&characterEncoding=utf-8
    jdbc.user=root
    jdbc.password=
    jdbc.acquireIncrement=3
    jdbc.initialPoolSize=10
    jdbc.minPoolSize=2
    jdbc.maxPoolSize=200
    jdbc.maxIdleTime=1000
    

    4、log4j.properties

    # Global logging configuration
    log4j.rootLogger=ERROR, stdout
    # MyBatis logging configuration...
    log4j.logger.com.hemi.controller=DEBUG
    log4j.logger.com.hemi.dao=DEBUG
    # 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
    

    spring事务配置

    applicationContext-tx.xml

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <aop:config>
            <aop:advisor advice-ref="txAdvicer" pointcut="execution(* com.hemi.service..*(..))"/>
        </aop:config>
    
        <tx:advice transaction-manager="transactionManager" id="txAdvicer">
            <tx:attributes>
                <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
                <tx:method name="delect*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="insert*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
    </beans>
    

    service层

    applicationContext-service.xml

    <context:component-scan base-package="com.hemi.service"/> 
    

    springmvc整合

    1、springmvc.xml

    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    

    2、web.xml

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             id="WebApp_ID" version="3.0">
        <display-name>appmanager</display-name>
        <!-- 监听器加载配置文件 -->
        <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>
    
        <!-- 解决乱码问题 -->
         <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>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
         </filter>
         <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
         </filter-mapping>
    
        <!-- 前端控制器 -->
        <servlet>
            <servlet-name>dispatherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
          </servlet>
          <servlet-mapping>
            <servlet-name>dispatherServlet</servlet-name>
            <url-pattern>/</url-pattern>
          </servlet-mapping>
    </web-app>
    

    mybatis分页插件

    <plugins>
            <!--分页插件-->
            <plugin interceptor="com.github.pagehelper.PageHelper"/>
    </plugins>
    

    相关文章

      网友评论

          本文标题:ssm

          本文链接:https://www.haomeiwen.com/subject/ysrcpxtx.html