zoukankan      html  css  js  c++  java
  • 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(一)

    Spring+MyBatis


     

      首先要搭建的是Spring+MyBatis的整合框架,毕竟Spring是整个Web框架的核心部位,而数据库操作是一切测试的基础嘛。

    目录结构


     

    ━java

      ┣ controller(控制层)

      ┣ mapper(因为没有Dao,用Mapper层替代持久层)

      ┣ pojo(基础模型层)

      ┣ service(业务层)

      ┗ util(通用工具)

    ━resource

      ┣config

        ┣mybatis(MyBatis配置,其实这里的配置文件啥内容也没有)

        ┣spring(Spring的配置)

      ┗mapper(用于存放Mybatis生成的mapper接口对应的xml配置文件)

     

    MyBatis配置文件


    目录:Resource/Config/mybatis,文件名:SqlMapConfig.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 
    6 <configuration>
    7 <--这里啥也不用写,因为我们整合Spring,配置都放在Spirng的配置中--!>
    8 </configuration>
    View Code

     

    数据库参数


    目录:resource/config,文件名:db.properties

    其中“db_house_rent”指的是你的数据库名称,直接替换即可,比如你的数据库叫"abc", 那你就改成:jdbc:mysql://127.0.0.1:3306/abc?characterEncoding=UTF-8。

    1 jdbc.driver = com.mysql.jdbc.Driver
    2 jdbc.url = jdbc:mysql://127.0.0.1:3306/db_house_rent?characterEncoding=UTF-8
    3 jdbc.username = root
    4 jdbc.password = 123456

    真正的MyBatis配置文件


    目录:resource/config/spring,文件名:applicationContext-dao.xml

    当然你也可以为了方便记忆,把文件名的dao改成mybatis,不影响我们后续的配置,但本文章的配置文件的文件名,是有统一格式要求的,后续会说为什么,但必须按照applicationContext-xxx.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--获取数据库配置文件-->
        <context:property-placeholder location="classpath:config/db.properties"/>
    
        <!--设置数据源c3p0-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="maxPoolSize" value="50"/>
            <property name="minPoolSize" value="2"/>
            <property name="maxIdleTime" value="60"/>
        </bean>
    
        <!--sqlsessionFactory bean-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
            <property name="dataSource" ref="dataSource"/>
            <!-- 显式指定Mapper文件位置,如果这里你不是按照我的目录结构来建设,那你要自己调整mapper的路径,这里说的mapper不是指java类,而是指对应的xml文件,如果不懂,先去学一下Mybatis的基本知识 -->
            <property name="mapperLocations">
                <list>
                    <value>classpath*:/mapper/*.xml</value>
                </list>
            </property>
        </bean>
    
        <!--自动扫描mapper接口,并注入sqlsession,这里顺带一提,因为这里自动扫描注册了,所以我们在mapper接口中,不需要使用注解@Repository去标注那些接口,同时也不要在配置文件中加入自动扫描context:component-scan的标签去扫描mapper包,否则会有矛盾-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.magic.rent.mapper"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSession"/>
        </bean>
    </beans> 

    MyBatis逆向工程配置文件


     目录:resource,文件名:generatorConfig.xml

    这个配置文件,是用于MyBatis的逆向工程的,这个虽然不属于本框架的内容,但是也算是Mybatis的一个非常好用的插件,所以也一并加到教程中来。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE generatorConfiguration
     3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
     5 
     6 <generatorConfiguration>
     7     <classPathEntry
     8             location="/Users/wuxinzhe/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar"/>
     9     <context id="testTables" targetRuntime="MyBatis3">
    10         <commentGenerator>
    11             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    12             <property name="suppressAllComments" value="true"/>
    13         </commentGenerator>
    14         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    15         <!--<jdbcConnection driverClass="${jdbc.driver}"-->
    16         <!--connectionURL="${jdbc.url}"-->
    17         <!--userId="${jdbc.username}"-->
    18         <!--password="${jdbc.password}">-->
    19         <!--</jdbcConnection>-->
    20         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    21                         connectionURL="jdbc:mysql://127.0.0.1:3306/db_house_rent?characterEncoding=UTF-8"
    22                         userId="root"
    23                         password="199176">
    24         </jdbcConnection>
    25 
    26         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
    27             NUMERIC 类型解析为java.math.BigDecimal -->
    28         <javaTypeResolver>
    29             <property name="forceBigDecimals" value="false"/>
    30         </javaTypeResolver>
    31 
    32         <!-- targetProject:生成PO类的位置 -->
    33         <javaModelGenerator targetPackage="mybatis.pojo"
    34                             targetProject="src/test/java">
    35             <!-- enableSubPackages:是否让schema作为包的后缀 -->
    36             <property name="enableSubPackages" value="false"/>
    37             <!-- 从数据库返回的值被清理前后的空格 -->
    38             <property name="trimStrings" value="true"/>
    39         </javaModelGenerator>
    40         <!-- targetProject:mapper映射文件生成的位置 -->
    41         <sqlMapGenerator targetPackage="mybatis.mapper"
    42                          targetProject="src/test/java">
    43             <!-- enableSubPackages:是否让schema作为包的后缀 -->
    44             <property name="enableSubPackages" value="false"/>
    45         </sqlMapGenerator>
    46         <!-- targetPackage:mapper接口生成的位置 -->
    47         <javaClientGenerator type="XMLMAPPER"
    48                              targetPackage="mybatis.mapper"
    49                              targetProject="src/test/java">
    50             <!-- enableSubPackages:是否让schema作为包的后缀 -->
    51             <property name="enableSubPackages" value="false"/>
    52         </javaClientGenerator>
    53         <!-- 指定数据库表,这些数据库表文件,是SpringSecurity的必备表,后续的文章会贴出SQL文件 -->
    54         <table tableName="SYS_USERS"
    55                enableCountByExample="false"
    56                enableUpdateByExample="false"
    57                enableDeleteByExample="false"
    58                enableSelectByExample="false"
    59                selectByExampleQueryId="false"/>
    60         <table tableName="SYS_ROLES"
    61                enableCountByExample="false"
    62                enableUpdateByExample="false"
    63                enableDeleteByExample="false"
    64                enableSelectByExample="false"
    65                selectByExampleQueryId="false"/>
    66         <table tableName="SYS_AUTHORITIES"
    67                enableCountByExample="false"
    68                enableUpdateByExample="false"
    69                enableDeleteByExample="false"
    70                enableSelectByExample="false"
    71                selectByExampleQueryId="false"/>
    72         <table tableName="SYS_MODULES"
    73                enableCountByExample="false"
    74                enableUpdateByExample="false"
    75                enableDeleteByExample="false"
    76                enableSelectByExample="false"
    77                selectByExampleQueryId="false"/>
    78         <table tableName="SYS_RESOURCES"
    79                enableCountByExample="false"
    80                enableUpdateByExample="false"
    81                enableDeleteByExample="false"
    82                enableSelectByExample="false"
    83                selectByExampleQueryId="false"/>
    84         <table tableName="PERSISTENT_LOGINS"
    85                enableCountByExample="false"
    86                enableUpdateByExample="false"
    87                enableDeleteByExample="false"
    88                enableSelectByExample="false"
    89                selectByExampleQueryId="false"/>
    90     </context>
    91 </generatorConfiguration>

      到此我要做一个解释,就是mapper逆向工程生成的文件,不是直接替换到java工程中,而是在本工程下,我建立的一个test目录下,这个专门用于写测试的,之所以要这么干,是因为如果当你修改数据库的时候,生成的文件会直接覆盖已有的文件,如果有修改这个自动生成的文件,那就会被覆盖掉,所以以防万一,还是这样比较方便,每次生成之后,对比一下再把新增的或修改的手动去替换。

    测试工程目录图


    ━java

      ┣ mybatis(用于存放生成的逆向工程文件) 

        ┣ com.magic.rent.service(这个是用JUnit自动生成的测试类,回头会说。)

        ┣ mapper(存放生成的mapper.java和mapper.xml文件)

        ┣ poco(存放生成的映射对象文件)

      ┗ test(很随意的测试都在这里,比如字符串截取之类的...)

    [如图,测试类的包,要在IDEA的配置中,设置为“绿色的测试资源”]

    [IDEA对项目工程的包有一些分门别类的设置,不懂的朋友自行百度了]

    [说实话,我是一个颜值党,Eclipse实在看不下去,所以喜欢IDEA没办法]

     

      既然配置完了,就是要怎么用了。打开IDEA右侧栏的Maven Project,找到插件组(Plugins)中的mybatis-generator.根据标号的操作步骤,找到了以后,直接“双击”即可。如果期间出了什么错,控制台会直接打印出错误原因,英语不好的朋友可以百度翻译。

  • 相关阅读:
    正则表达式 ^
    jQuery的加法运算,val()获取的结果相加变成了字符串连接。
    mssql 取数据指定条数(例:100-200条的数据)
    css样式大全(整理版)
    50个技巧提高你的PHP网站程序执行效率
    ASP版_阿里大于短信API Demo
    FusionCharts的使用方法(超详细)
    FusionCharts参数说明 (中文)
    web服务器选择Apache还是Nginx
    反向代理服务器的工作原理
  • 原文地址:https://www.cnblogs.com/wuxinzhe/p/5922478.html
Copyright © 2011-2022 走看看