【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

新建一个配置文件,内容如下:

 <?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>
<--这里啥也不用写,因为我们整合Spring,配置都放在Spirng的配置中--!>
</configuration>

数据库参数


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

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

 jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/db_house_rent?characterEncoding=UTF-8
jdbc.username = root
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的一个非常好用的插件,所以也一并加到教程中来。

 <?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
location="/Users/wuxinzhe/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar"/>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!--<jdbcConnection driverClass="${jdbc.driver}"-->
<!--connectionURL="${jdbc.url}"-->
<!--userId="${jdbc.username}"-->
<!--password="${jdbc.password}">-->
<!--</jdbcConnection>-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/db_house_rent?characterEncoding=UTF-8"
userId="root"
password="199176">
</jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="mybatis.pojo"
targetProject="src/test/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mybatis.mapper"
targetProject="src/test/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="mybatis.mapper"
targetProject="src/test/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表,这些数据库表文件,是SpringSecurity的必备表,后续的文章会贴出SQL文件 -->
<table tableName="SYS_USERS"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="SYS_ROLES"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="SYS_AUTHORITIES"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="SYS_MODULES"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="SYS_RESOURCES"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="PERSISTENT_LOGINS"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>

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

测试工程目录图


━java

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

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

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

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

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

【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(一)

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

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

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

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

【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(一)

上一篇:【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(二)


下一篇:python中str和byte的相互转化