搭建SSM底层框架
1. 利用mybatis反向工程generatorSqlmapCustom完成对数据库十表的映射
generatorConfig.xml
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 <context id="testTables" targetRuntime="MyBatis3">
8 <commentGenerator>
9
10 <property name="suppressAllComments" value="true" />
11 commentGenerator>
12
13 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
14 connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
15 password="root">
16 jdbcConnection>
17
19 <javaTypeResolver>
20 <property name="forceBigDecimals" value="false" />
21 javaTypeResolver>
22
23
24 <javaModelGenerator targetPackage="org.guangsoft.po"
25 targetProject=".\src">
26
27 <property name="enableSubPackages" value="false" />
28
29 <property name="trimStrings" value="true" />
30 javaModelGenerator>
31
32 <sqlMapGenerator targetPackage="mybatis"
33 targetProject=".\src">
34
35 <property name="enableSubPackages" value="false" />
36 sqlMapGenerator>
37
38 <javaClientGenerator type="XMLMAPPER"
39 targetPackage="org.guangsoft.mapper"
40 targetProject=".\src">
41
42 <property name="enableSubPackages" value="false" />
43 javaClientGenerator>
44
45 <table schema="" tableName="users">table>
46 <table schema="" tableName="roles">table>
47 <table schema="" tableName="privileges">table>
48 <table schema="" tableName="items">table>
49 <table schema="" tableName="cars">table>
50 <table schema="" tableName="clients">table>
51 <table schema="" tableName="loginlog">table>
52 <table schema="" tableName="systemlog">table>
53 <table schema="" tableName="rents">table>
54 <table schema="" tableName="checks">table>
55 context>
56 generatorConfiguration>
2. 配置mybatis
SqlMapperConfig.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
7 <plugins>
8 <plugin interceptor="com.github.pagehelper.PageHelper">
9
10 <property name="dialect" value="mysql"/>
11 plugin>
12 plugins>
13 configuration>
3. 配置spring-dao
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
applicationContext-dao.xml
1 xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10
11 <context:property-placeholder location="classpath:resource/*.properties"/>
12
13
14 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
15
16 <property name="driverClassName" value="${jdbc.driver}" />
17
18 <property name="url" value="${jdbc.url}" />
19 <property name="username" value="${jdbc.username}" />
20 <property name="password" value="${jdbc.password}" />
21
22 <property name="initialSize" value="1" />
23 <property name="minIdle" value="1" />
24 <property name="maxActive" value="20" />
25
26 <property name="maxWait" value="60000" />
27
28 <property name="timeBetweenEvictionRunsMillis" value="60000" />
29
30 <property name="minEvictableIdleTimeMillis" value="300000" />
31 <property name="validationQuery" value="SELECT 'x'" />
32 <property name="testWhileIdle" value="true" />
33 <property name="testOnBorrow" value="false" />
34 <property name="testOnReturn" value="false" />
35
36 <property name="poolPreparedStatements" value="false" />
37 <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
38
39 <property name="filters" value="stat" />
40 bean>
41
42
43 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
44 <property name="configLocation" value="classpath:mybatis/SqlMapperConfig.xml"/>
45 <property name="dataSource" ref="dataSource"/>
46 bean>
47
48
49 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
50 <property name="basePackage" value="mybatis"/>
51 bean>
52 beans>
applicationContext-service.xml
1 xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10 <context:component-scan base-package="org.guangsoft.service"/>
11 beans>
applicationContext-tarns.xml
1 xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10
11 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
12 <property name="dataSource" ref="dataSource"/>
13 bean>
14
15
16 <tx:advice id="Advice" transaction-manager="transactionManager">
17
18 <tx:attributes>
19
20 <tx:method name="save*" propagation="REQUIRED" />
21 <tx:method name="insert*" propagation="REQUIRED" />
22 <tx:method name="add*" propagation="REQUIRED" />
23 <tx:method name="create*" propagation="REQUIRED" />
24 <tx:method name="delete*" propagation="REQUIRED" />
25 <tx:method name="update*" propagation="REQUIRED" />
26 <tx:method name="drop*" propagation="REQUIRED" />
27 <tx:method name="modify*" propagation="REQUIRED" />
28 <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
29 <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
30 <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
31 tx:attributes>
32 tx:advice>
33 <aop:config>
34 <aop:advisor advice-ref="Advice" pointcut="execution(* org.guangsoft.service.*.*(..))" />
35 aop:config>
36 beans>
springmvc.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" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9
10 <context:component-scan base-package="org.guangsoft.controller"/>
11
12
13 <mvc:annotation-driven>mvc:annotation-driven>
14
15
16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17 <property name="prefix" value="/" />
18 <property name="suffix" value=".jsp" />
19 bean>
20 beans>
4. 配置web.xml
1 xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6
7 <context-param>
8 <param-name>contextConfigLocationparam-name>
9 <param-value>classpath:spring/applicationContext-*.xmlparam-value>
10 context-param>
11 <listener>
12 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
13 listener>
14
15
16 <filter>
17 <filter-name>CharacterEncodingFilterfilter-name>
18 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
19 <init-param>
20 <param-name>encodingparam-name>
21 <param-value>utf-8param-value>
22 init-param>
23 filter>
24 <filter-mapping>
25 <filter-name>CharacterEncodingFilterfilter-name>
26 <url-pattern>/*url-pattern>
27 filter-mapping>
28
29
30 <servlet>
31 <servlet-name>guangsoftservlet-name>
32 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
33
34 <init-param>
35 <param-name>contextConfigLocationparam-name>
36 <param-value>classpath:spring/springmvc.xmlparam-value>
37 init-param>
38 <load-on-startup>1load-on-startup>
39 servlet>
40
41 <servlet-mapping>
42 <servlet-name>guangsoftservlet-name>
43 <url-pattern>*.dourl-pattern>
44 servlet-mapping>
45
46 <welcome-file-list>
47 <welcome-file>index.jspwelcome-file>
48 welcome-file-list>
49 web-app>