码迷,mamicode.com
首页 > 其他好文 > 详细

SSM框架整合

时间:2018-04-26 12:26:08      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:引擎   eth   ted   index.jsp   生成   console   删除用户   hone   实体映射   

项目描述:本项目是基本的ssm整合,基本的增删改查,仅供学习和参考!

Spring + SpringMVC + Mybatis +  Freemarker

1、项目目录

技术分享图片

2、系统介绍

系统代码规范参考《阿里巴巴Java开发手册终极版v1.3.0》

系统架构:Spring + SpringMVC + MyBatis
前端模板引擎:freemarker
数据源连接池:Druid
日志框架:log4j
测试框架:Junit4
项目管理:maven

数据库:mysql5.6

jdk版本:jdk1.8

开发服务器:Tomcat 8.0

功能:增删改查

系统结构介绍:
系统实现路径:src/main/java
系统测试路径:src/main/test
静态资源及配置文件路径:src/main/resources

包名:
com.elanbase.hr.common
com.elanbase.hr.config
com.elanbase.hr.controller 
com.elanbase.hr.mapper
com.elanbase.hr.model
com.elanbase.hr.service
com.elanbase.hr.service.impl

html页面路径:webapp
freemarker模板路径:webapp/WEB-INF/ftl

3、配置文件

3.1 Maven配置

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.elanbasehr</groupId>
  5     <artifactId>elanbasehr</artifactId>
  6     <version>0.0.1-SNAPSHOT</version>
  7     <packaging>war</packaging>
  8     <name>elanbasehr</name>
  9 
 10     <properties>
 11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 12         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 13         <java.version>1.8</java.version>
 14         <junit.version>4.9</junit.version>
 15         <spring.version>4.3.10.RELEASE</spring.version>
 16         <mybatis.version>3.4.4</mybatis.version>
 17         <mybatis.spring.version>1.3.1</mybatis.spring.version>
 18         <jstl.version>1.2</jstl.version>
 19         <log4j.version>1.2.17</log4j.version>
 20         <fastjson.version>1.2.35</fastjson.version>
 21         <slf4j.version>1.7.25</slf4j.version>
 22         <jackson.version>1.9.13</jackson.version>
 23         <commons-fileupload.version>1.3.3</commons-fileupload.version>
 24         <commons-io.version>2.5</commons-io.version>
 25         <commons-codec.version>1.10</commons-codec.version>
 26         <commons-dbcp.version>1.4</commons-dbcp.version>
 27     </properties>
 28 
 29     <dependencies>
 30         <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
 31         <dependency>
 32             <groupId>com.alibaba</groupId>
 33             <artifactId>druid</artifactId>
 34             <version>1.1.8</version>
 35         </dependency>
 36         <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
 37         <dependency>
 38             <groupId>org.springframework.boot</groupId>
 39             <artifactId>spring-boot-starter-web</artifactId>
 40             <version>1.5.12.RELEASE</version>
 41         </dependency>
 42 
 43         <dependency>
 44             <groupId>org.springframework.boot</groupId>
 45             <artifactId>spring-boot-configuration-processor</artifactId>
 46             <version>2.0.1.RELEASE</version>
 47         </dependency>
 48 
 49         <!-- freeMarker -->
 50         <dependency>
 51             <groupId>org.freemarker</groupId>
 52             <artifactId>freemarker</artifactId>
 53             <version>2.3.23</version>
 54         </dependency>
 55 
 56         <!-- Junit测试 -->
 57         <dependency>
 58             <groupId>junit</groupId>
 59             <artifactId>junit</artifactId>
 60             <version>${junit.version}</version>
 61             <scope>test</scope>
 62         </dependency>
 63 
 64         <!-- spring包 core、web、oxm、tx、jdbc、webmvc、aop、context、test -->
 65         <dependency>
 66             <groupId>org.springframework</groupId>
 67             <artifactId>spring-core</artifactId>
 68             <version>${spring.version}</version>
 69         </dependency>
 70         <dependency>
 71             <groupId>org.springframework</groupId>
 72             <artifactId>spring-web</artifactId>
 73             <version>${spring.version}</version>
 74         </dependency>
 75         <dependency>
 76             <groupId>org.springframework</groupId>
 77             <artifactId>spring-oxm</artifactId>
 78             <version>${spring.version}</version>
 79         </dependency>
 80         <dependency>
 81             <groupId>org.springframework</groupId>
 82             <artifactId>spring-tx</artifactId>
 83             <version>${spring.version}</version>
 84         </dependency>
 85         <dependency>
 86             <groupId>org.springframework</groupId>
 87             <artifactId>spring-jdbc</artifactId>
 88             <version>${spring.version}</version>
 89         </dependency>
 90         <dependency>
 91             <groupId>org.springframework</groupId>
 92             <artifactId>spring-webmvc</artifactId>
 93             <version>${spring.version}</version>
 94         </dependency>
 95         <dependency>
 96             <groupId>org.springframework</groupId>
 97             <artifactId>spring-aop</artifactId>
 98             <version>${spring.version}</version>
 99         </dependency>
100 
101         <dependency>
102             <groupId>org.springframework</groupId>
103             <artifactId>spring-context-support</artifactId>
104             <version>${spring.version}</version>
105         </dependency>
106         <dependency>
107             <groupId>org.springframework</groupId>
108             <artifactId>spring-test</artifactId>
109             <version>${spring.version}</version>
110         </dependency>
111         <!-- mybatis核心包 -->
112         <dependency>
113             <groupId>org.mybatis</groupId>
114             <artifactId>mybatis</artifactId>
115             <version>${mybatis.version}</version>
116         </dependency>
117         <!-- mybatis/spring包 -->
118         <dependency>
119             <groupId>org.mybatis</groupId>
120             <artifactId>mybatis-spring</artifactId>
121             <version>${mybatis.spring.version}</version>
122         </dependency>
123 
124         <!-- 导入Mysql数据库链接jar包 -->
125         <dependency>
126             <groupId>mysql</groupId>
127             <artifactId>mysql-connector-java</artifactId>
128             <version>5.1.38</version>
129         </dependency>
130 
131         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
132         <dependency>
133             <groupId>commons-dbcp</groupId>
134             <artifactId>commons-dbcp</artifactId>
135             <version>${commons-dbcp.version}</version>
136         </dependency>
137         <!-- JSTL标签类 -->
138         <dependency>
139             <groupId>jstl</groupId>
140             <artifactId>jstl</artifactId>
141             <version>${jstl.version}</version>
142         </dependency>
143         <!-- 日志文件管理包 -->
144         <dependency>
145             <groupId>log4j</groupId>
146             <artifactId>log4j</artifactId>
147             <version>${log4j.version}</version>
148         </dependency>
149 
150         <!-- 格式化对象,方便输出日志 -->
151         <dependency>
152             <groupId>com.alibaba</groupId>
153             <artifactId>fastjson</artifactId>
154             <version>${fastjson.version}</version>
155         </dependency>
156 
157         <dependency>
158             <groupId>org.slf4j</groupId>
159             <artifactId>slf4j-api</artifactId>
160             <version>${slf4j.version}</version>
161         </dependency>
162 
163         <dependency>
164             <groupId>org.slf4j</groupId>
165             <artifactId>slf4j-log4j12</artifactId>
166             <version>${slf4j.version}</version>
167         </dependency>
168         <!-- json和bean之间相互转换 -->
169         <dependency>
170             <groupId>org.codehaus.jackson</groupId>
171             <artifactId>jackson-core-asl</artifactId>
172             <version>${jackson.version}</version>
173         </dependency>
174         <dependency>
175             <groupId>org.codehaus.jackson</groupId>
176             <artifactId>jackson-mapper-asl</artifactId>
177             <version>${jackson.version}</version>
178         </dependency>
179         <!-- 上传组件包 -->
180         <dependency>
181             <groupId>commons-fileupload</groupId>
182             <artifactId>commons-fileupload</artifactId>
183             <version>${commons-fileupload.version}</version>
184         </dependency>
185         <dependency>
186             <groupId>commons-io</groupId>
187             <artifactId>commons-io</artifactId>
188             <version>${commons-io.version}</version>
189         </dependency>
190         <dependency>
191             <groupId>commons-codec</groupId>
192             <artifactId>commons-codec</artifactId>
193             <version>${commons-codec.version}</version>
194         </dependency>
195 
196         <dependency>
197             <groupId>javax.servlet</groupId>
198             <artifactId>javax.servlet-api</artifactId>
199             <version>4.0.0-b07</version>
200             <scope>provided</scope>
201         </dependency>
202     </dependencies>
203 
204     <build>
205         <testSourceDirectory>src/main/test</testSourceDirectory>
206         <resources>
207             <resource>
208                 <directory>src/main/java</directory>
209                 <excludes>
210                     <exclude>**/*.java</exclude>
211                 </excludes>
212             </resource>
213             <resource>
214                 <directory>src/main/resources</directory>
215                 <excludes>
216                     <exclude>**/*.java</exclude>
217                 </excludes>
218             </resource>
219         </resources>
220         <plugins>
221             <plugin>
222                 <artifactId>maven-compiler-plugin</artifactId>
223                 <version>3.5.1</version>
224                 <configuration>
225                     <source>1.8</source>
226                     <target>1.8</target>
227                 </configuration>
228             </plugin>
229             <plugin>
230                 <artifactId>maven-war-plugin</artifactId>
231                 <version>3.0.0</version>
232                 <configuration>
233                     <warSourceDirectory>webapp</warSourceDirectory>
234                 </configuration>
235             </plugin>
236         </plugins>
237     </build>
238 </project>

 3.2 Mybatis配置(可选):mybatis-config.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     <settings>
 8         <!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认为false -->
 9         <setting name="mapUnderscoreToCamelCase" value="true" />
10     </settings>
11 
12     <!-- mybatis别名定义 -->
13     <!-- <typeAliases>
14         <typeAlias type="com.elanbase.hr.model.SysUser" alias="SysUser" />
15     </typeAliases> -->
16 
17 </configuration>

3.3 Spring集成Mybatis配置:spring-mybatis.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" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://www.springframework.org/schema/tx
11         http://www.springframework.org/schema/tx/spring-tx.xsd">
12 
13     <!-- 自动扫描 -->
14     <context:component-scan base-package="com.elanbase.hr.service" />
15 
16     <!-- 阿里 druid数据库连接池 -->
17     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
18          <!-- 数据库基本信息配置 -->
19         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
20         <property name="url" value="jdbc:mysql://localhost:3306/elanbasehr?characterEncoding=utf-8" />
21         <property name="username" value="root" />
22         <property name="password" value="root" />
23         <property name="filters" value="stat" />  
24          <!-- 最大并发连接数 -->
25          <property name="maxActive" value="20" />
26          <!-- 初始化连接数量 -->
27          <property name="initialSize" value="1" />
28          <!-- 配置获取连接等待超时的时间 -->
29          <property name="maxWait" value="60000" />
30          <!-- 最小空闲连接数 -->
31          <property name="minIdle" value="10" />
32          <!-- 最大空闲连接数 -->
33          <property name="maxIdle" value="15" />  
34          <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
35          <property name="timeBetweenEvictionRunsMillis" value="60000" />
36          <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
37          <property name="minEvictableIdleTimeMillis" value="300000" />  
38          <property name="validationQuery" value="SELECT" />  
39          <property name="testWhileIdle" value="true" />  
40          <property name="testOnBorrow" value="false" />  
41          <property name="testOnReturn" value="false" />  
42          <property name="maxOpenPreparedStatements" value="20" />
43          <!-- 打开removeAbandoned功能 -->
44          <property name="removeAbandoned" value="true" />
45          <!-- 1800秒,也就是30分钟 -->
46          <property name="removeAbandonedTimeout" value="1800" />
47          <!-- 关闭abanded连接时输出错误日志 -->   
48          <property name="logAbandoned" value="true" />
49     </bean>
50 
51     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
52     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
53         <property name="dataSource" ref="dataSource" />
54         <property name="configLocation" value="classpath:mybatis-config.xml" />
55         <!-- 自动扫描mapping.xml文件 -->
56         <property name="mapperLocations" value="classpath:com/elanbase/hr/mapper/*.xml"></property>
57     </bean>
58 
59     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
60     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
61         <property name="sqlSessionFactory" ref="sqlSessionFactory" />
62         <property name="basePackage" value="com.elanbase.hr.mapper" />
63     </bean>
64 
65     <!-- 事务管理 -->
66     <bean id="txManager"
67         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
68         <property name="dataSource" ref="dataSource" />
69     </bean>
70 
71     <!-- 配置事务通知属性 -->
72     <tx:advice id="txAdvice" transaction-manager="txManager">
73         <!-- 定义事务传播属性 -->
74         <tx:attributes>
75             <tx:method name="insert*" propagation="REQUIRED" />
76             <tx:method name="update*" propagation="REQUIRED" />
77             <tx:method name="edit*" propagation="REQUIRED" />
78             <tx:method name="save*" propagation="REQUIRED" />
79             <tx:method name="add*" propagation="REQUIRED" />
80             <tx:method name="new*" propagation="REQUIRED" />
81             <tx:method name="set*" propagation="REQUIRED" />
82             <tx:method name="remove*" propagation="REQUIRED" />
83             <tx:method name="delete*" propagation="REQUIRED" />
84             <tx:method name="change*" propagation="REQUIRED" />
85             <tx:method name="get*" propagation="REQUIRED" read-only="true" />
86             <tx:method name="find*" propagation="REQUIRED" read-only="true" />
87             <tx:method name="select*" propagation="REQUIRED" read-only="true" />
88             <tx:method name="load*" propagation="REQUIRED" read-only="true" />
89             <tx:method name="*" propagation="REQUIRED" read-only="true" />
90         </tx:attributes>
91     </tx:advice>
92 
93 
94     <!-- 配置事务切面 -->
95     <!-- <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* 
96         com.ssm.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" 
97         /> </aop:config> -->
98 
99 </beans>

3.4 Spring集成SpringMVC配置:spring-mvc.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:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6       http://www.springframework.org/schema/beans/spring-beans.xsd
 7       http://www.springframework.org/schema/context
 8       http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9       http://www.springframework.org/schema/mvc
10      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
11 
12     <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
13     <context:component-scan base-package="com.elanbase.hr.controller" />
14 
15     <!-- 开启注解 -->
16     <mvc:annotation-driven />
17 
18     <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
19     <mvc:resources mapping="/img/**" location="/img/" />
20     <mvc:resources mapping="/images/**" location="/images/" />
21     <mvc:resources mapping="/js/**" location="/js/" />
22     <mvc:resources mapping="/css/**" location="/css/" />
23     <mvc:resources mapping="/fonts/**" location="/fonts/" />
24 
25     <!-- 配置freeMarker视图解析器 -->
26     <bean id="viewResolverFtl"
27         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
28         <property name="viewClass"
29             value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
30         <property name="contentType" value="text/html; charset=utf-8" />
31         <property name="cache" value="true" />
32         <property name="suffix" value=".html" />
33         <property name="order" value="0" />
34     </bean>
35 
36     <!-- 配置freeMarker的模板路径 -->
37     <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
38         <property name="templateLoaderPath" value="" />
39     </bean>
40 
41 </beans>

3.5 日志配置:log4j.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 3 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 4     <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
 5         <layout class="org.apache.log4j.PatternLayout">
 6             <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
 7         </layout>
 8     </appender>
 9 
10     <logger name="java.sql">
11         <level value="ERROR" />
12     </logger>
13 
14     <logger name="org.apache.ibatis">
15         <level value="ERROR" />
16     </logger>
17 
18     <root>
19         <level value="WARN" />
20         <appender-ref ref="STDOUT" />
21     </root>
22 
23 </log4j:configuration>

3.6 web.xml 配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5     id="WebApp_ID" version="3.1">
 6     <display-name>elanbasehr</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.html</welcome-file>
 9         <welcome-file>index.jsp</welcome-file>
10     </welcome-file-list>
11 
12     <!-- 加载Spring容器配置 -->
13     <listener>
14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15     </listener>
16 
17     <!-- 加载 Spring 配置文件 -->
18     <context-param>
19         <param-name>contextConfigLocation</param-name>
20         <param-value>classpath*:spring-mybatis.xml</param-value>
21     </context-param>
22 
23     <!-- 加载 SpringMVC 配置文件 -->
24     <servlet>
25         <servlet-name>springMVC</servlet-name>
26         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
27         <init-param>
28             <param-name>contextConfigLocation</param-name>
29             <param-value>classpath*:spring-mvc.xml</param-value>
30         </init-param>
31         <load-on-startup>1</load-on-startup>
32     </servlet>
33     <servlet-mapping>
34         <servlet-name>springMVC</servlet-name>
35         <url-pattern>/</url-pattern>
36     </servlet-mapping>
37      <servlet-mapping>
38         <servlet-name>default</servlet-name>
39         <url-pattern>*.html</url-pattern>
40         <url-pattern>*.js</url-pattern>
41         <url-pattern>*.css</url-pattern>
42     </servlet-mapping>
43 
44     <!-- 连接池 启用Web监控统计功能   start-->
45     <filter>
46         <filter-name>DruidWebStatFilter</filter-name>
47         <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
48         <init-param>
49             <param-name>exclusions</param-name> <!-- 经常需要排除一些不必要的url -->
50             <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
51         </init-param>
52     </filter>
53     <filter-mapping>
54         <filter-name>DruidWebStatFilter</filter-name>
55         <url-pattern>/*</url-pattern>
56     </filter-mapping>
57     <servlet>
58         <servlet-name>DruidStatView</servlet-name>
59         <!-- 这个StatViewServlet的用途包括:提供监控信息展示的html页面;提供监控信息的JSON API -->
60         <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
61     </servlet>
62     <servlet-mapping>
63         <servlet-name>DruidStatView</servlet-name>
64         <url-pattern>/druid/*</url-pattern>
65     </servlet-mapping>
66     <!-- 连接池 启用Web监控统计功能   end-->
67     
68     
69 </web-app>

3.7 Mybatis Generator 基本配置(可选):mybatis-generator.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 <generatorConfiguration>
 6     <context id="DB2Tables" targetRuntime="MyBatis3">
 7         <commentGenerator>
 8             <property name="suppressDate" value="true" />
 9             <property name="suppressAllComments" value="true" />
10         </commentGenerator>
11 
12         <!--数据库链接地址账号密码 -->
13         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
14             connectionURL="jdbc:mysql://localhost:3306/elanbasehr?characterEncoding=UTF-8" userId="root" password="root">
15         </jdbcConnection>
16         <javaTypeResolver>
17             <property name="forceBigDecimals" value="false" />
18         </javaTypeResolver>
19         
20         <!--生成Model类存放位置 -->
21         <javaModelGenerator targetPackage="com.elanbase.hr.model" targetProject="elanbasehr">
22             <property name="enableSubPackages" value="true" />
23             <property name="trimStrings" value="true" />
24         </javaModelGenerator>
25         <!--生成映射文件xml存放位置 -->
26         <sqlMapGenerator targetPackage="com.elanbase.hr.mapper" targetProject="elanbasehr">
27             <property name="enableSubPackages" value="true" />
28         </sqlMapGenerator>
29         <!--生成Dao类存放位置 -->
30         <javaClientGenerator type="MIXEDMAPPER" targetPackage="com.elanbase.hr.mapper" targetProject="elanbasehr">
31             <property name="enableSubPackages" value="true" />
32         </javaClientGenerator>
33         
34         <!--生成对应表及类名 -->
35         <table tableName="sys_user" domainObjectName="SysUser"
36             enableCountByExample="false" enableUpdateByExample="false"
37             enableDeleteByExample="false" enableSelectByExample="false"
38             selectByExampleQueryId="false">
39         </table>
40             
41     </context>
42 </generatorConfiguration>

 4、数据库:elanbasehr.sql

 1 /*
 2 Navicat MySQL Data Transfer
 3 
 4 Source Server         : localhost
 5 Source Server Version : 50717
 6 Source Host           : localhost:3306
 7 Source Database       : elanbasehr
 8 
 9 Target Server Type    : MYSQL
10 Target Server Version : 50717
11 File Encoding         : 65001
12 
13 Date: 2018-04-24 11:50:06
14 */
15 
16 SET FOREIGN_KEY_CHECKS=0;
17 
18 -- ----------------------------
19 -- Table structure for sys_user
20 -- ----------------------------
21 DROP TABLE IF EXISTS `sys_user`;
22 CREATE TABLE `sys_user` (
23   `id` bigint(19) NOT NULL AUTO_INCREMENT COMMENT 主键id,
24   `login_name` varchar(64) NOT NULL COMMENT 登录名,
25   `name` varchar(64) NOT NULL COMMENT 用户名,
26   `password` varchar(64) NOT NULL COMMENT 密码,
27   `sex` tinyint(2) DEFAULT 0 COMMENT 性别,
28   `age` tinyint(2) DEFAULT 0 COMMENT 年龄,
29   `phone` varchar(20) DEFAULT NULL COMMENT 手机号,
30   `user_type` tinyint(2) DEFAULT 0 COMMENT 用户类别,
31   `status` tinyint(2) DEFAULT 0 COMMENT 用户状态,
32   `del_flag` tinyint(1) DEFAULT NULL COMMENT 删除标记,
33   `update_time` datetime DEFAULT NULL COMMENT 更新时间,
34   `create_time` datetime DEFAULT NULL COMMENT 创建时间,
35   PRIMARY KEY (`id`)
36 ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 COMMENT=用户;
37 
38 -- ----------------------------
39 -- Records of sys_user
40 -- ----------------------------
41 INSERT INTO `sys_user` VALUES (38, admin, 系统管理员, 123456, 0, null, ‘‘, 0, 0, 0, 2018-04-24 10:52:59, 2018-04-24 10:52:59);
42 INSERT INTO `sys_user` VALUES (42, test, 测试, 123456, 0, null, ‘‘, 0, 0, 0, 2018-04-24 21:46:39, 2018-04-24 21:34:54);

5、代码实现

com.elanbase.hr.model 实体类包

SysUser.java

  1 package com.elanbase.hr.model;
  2 
  3 import java.util.Date;
  4 
  5 public class SysUser {
  6     
  7     private Integer id;
  8 
  9     private String loginName;
 10 
 11     private String name;
 12 
 13     private String password;
 14 
 15     private Integer sex;
 16 
 17     private Integer age;
 18 
 19     private String phone;
 20 
 21     private Integer userType;
 22 
 23     private Integer status;
 24 
 25     private Integer delFlag;
 26 
 27     private Date updateTime;
 28 
 29     private Date createTime;
 30 
 31     public Integer getId() {
 32         return id;
 33     }
 34 
 35     public void setId(Integer id) {
 36         this.id = id;
 37     }
 38 
 39     public String getLoginName() {
 40         return loginName;
 41     }
 42 
 43     public void setLoginName(String loginName) {
 44         this.loginName = loginName == null ? null : loginName.trim();
 45     }
 46 
 47     public String getName() {
 48         return name;
 49     }
 50 
 51     public void setName(String name) {
 52         this.name = name == null ? null : name.trim();
 53     }
 54 
 55     public String getPassword() {
 56         return password;
 57     }
 58 
 59     public void setPassword(String password) {
 60         this.password = password == null ? null : password.trim();
 61     }
 62 
 63     public Integer getSex() {
 64         return sex;
 65     }
 66 
 67     public void setSex(Integer sex) {
 68         this.sex = sex;
 69     }
 70 
 71     public Integer getAge() {
 72         return age;
 73     }
 74 
 75     public void setAge(Integer age) {
 76         this.age = age;
 77     }
 78 
 79     public String getPhone() {
 80         return phone;
 81     }
 82 
 83     public void setPhone(String phone) {
 84         this.phone = phone == null ? null : phone.trim();
 85     }
 86 
 87     public Integer getUserType() {
 88         return userType;
 89     }
 90 
 91     public void setUserType(Integer userType) {
 92         this.userType = userType;
 93     }
 94 
 95     public Integer getStatus() {
 96         return status;
 97     }
 98 
 99     public void setStatus(Integer status) {
100         this.status = status;
101     }
102 
103     public Integer getDelFlag() {
104         return delFlag;
105     }
106 
107     public void setDelFlag(Integer delFlag) {
108         this.delFlag = delFlag;
109     }
110 
111     public Date getUpdateTime() {
112         return updateTime;
113     }
114 
115     public void setUpdateTime(Date updateTime) {
116         this.updateTime = updateTime;
117     }
118 
119     public Date getCreateTime() {
120         return createTime;
121     }
122 
123     public void setCreateTime(Date createTime) {
124         this.createTime = createTime;
125     }
126 }

com.elanbase.hr.mapper 实体映射类 和 dao接口包

SysUserMapper.java

 1 package com.elanbase.hr.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.elanbase.hr.model.SysUser;
 6 
 7 public interface SysUserMapper {
 8 
 9     SysUser getByPrimaryKey(Integer id);
10 
11     List<SysUser> listSysUser();
12 
13     int insertSysUser(SysUser record);
14 
15     int deleteByPrimaryKey(Integer id);
16 
17     int updateByPrimaryKey(SysUser record);
18 
19 }

SysUserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.elanbase.hr.mapper.SysUserMapper">
 4     <resultMap id="BaseResultMap" type="com.elanbase.hr.model.SysUser">
 5         <id column="id" jdbcType="INTEGER" property="id" />
 6         <result column="login_name" jdbcType="VARCHAR" property="loginName" />
 7         <result column="name" jdbcType="VARCHAR" property="name" />
 8         <result column="password" jdbcType="VARCHAR" property="password" />
 9         <result column="sex" jdbcType="INTEGER" property="sex" />
10         <result column="age" jdbcType="INTEGER" property="age" />
11         <result column="phone" jdbcType="VARCHAR" property="phone" />
12         <result column="user_type" jdbcType="INTEGER" property="userType" />
13         <result column="status" jdbcType="INTEGER" property="status" />
14         <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
15         <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
16         <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
17     </resultMap>
18     <sql id="Base_Column_List">
19         id, login_name, name, password, sex, age, phone, user_type,
20         status,
21         del_flag, update_time,
22         create_time
23     </sql>
24 
25     <select id="getByPrimaryKey" parameterType="Integer" resultType="com.elanbase.hr.model.SysUser">
26         select * from sys_user where id = #{id}
27     </select>
28 
29     <select id="listSysUser" resultType="com.elanbase.hr.model.SysUser">
30         select * from sys_user
31     </select>
32 
33     <insert id="insertSysUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.elanbase.hr.model.SysUser">
34         insert into sys_user (login_name,name,password)
35         values (#{loginName},#{name},#{password})
36     </insert>
37 
38     <delete id="deleteByPrimaryKey" parameterType="Integer">
39         delete from sys_user where id = #{id}
40     </delete>
41 
42     <update id="updateByPrimaryKey" parameterType="com.elanbase.hr.model.SysUser">
43         update sys_user set login_name=#{loginName},name=#{name} where id = #{id}
44     </update>
45     
46 </mapper>

com.elanbase.hr.service 业务接口包

SysUserService.java

 1 package com.elanbase.hr.service;
 2 
 3 import java.util.List;
 4 
 5 import com.elanbase.hr.model.SysUser;
 6 
 7 public interface SysUserService {
 8 
 9     SysUser getByPrimaryKey(Integer id);
10 
11     List<SysUser> listSysUser();
12 
13     int insertSysUser(SysUser record);
14 
15     int deleteByPrimaryKey(Integer id);
16 
17     int updateByPrimaryKey(SysUser record);
18 }

com.elanbase.hr.service.impl 业务实现类包

SysUserServiceImpl.java

 1 package com.elanbase.hr.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.springframework.stereotype.Service;
 8 
 9 import com.elanbase.hr.mapper.SysUserMapper;
10 import com.elanbase.hr.model.SysUser;
11 import com.elanbase.hr.service.SysUserService;
12 
13 @Service
14 public class SysUserServiceImpl implements SysUserService {
15 
16     @Resource
17     private SysUserMapper userMapper;
18 
19     @Override
20     public SysUser getByPrimaryKey(Integer id) {
21         return userMapper.getByPrimaryKey(id);
22     }
23 
24     @Override
25     public List<SysUser> listSysUser() {
26         return userMapper.listSysUser();
27     }
28 
29     @Override
30     public int insertSysUser(SysUser record) {
31         return userMapper.insertSysUser(record);
32     }
33 
34     @Override
35     public int deleteByPrimaryKey(Integer id) {
36         return userMapper.deleteByPrimaryKey(id);
37     }
38 
39     @Override
40     public int updateByPrimaryKey(SysUser record) {
41         return userMapper.updateByPrimaryKey(record);
42     }
43 
44 }

com.elanbase.hr.controller 控制层包

SysUserController.java

  1 package com.elanbase.hr.controller;
  2 
  3 import java.util.List;
  4 import javax.annotation.Resource;
  5 import org.springframework.stereotype.Controller;
  6 import org.springframework.ui.Model;
  7 import org.springframework.web.bind.annotation.RequestMapping;
  8 import org.springframework.web.bind.annotation.RequestMethod;
  9 import org.springframework.web.bind.annotation.RequestParam;
 10 import org.springframework.web.servlet.ModelAndView;
 11 
 12 import com.elanbase.hr.model.SysUser;
 13 import com.elanbase.hr.service.SysUserService;
 14 
 15 @Controller
 16 @RequestMapping(value = "user")
 17 public class SysUserController {
 18 
 19     @Resource
 20     private SysUserService userService;
 21 
 22     /**
 23      * 获取用户对象
 24      * 
 25      * @param mav
 26      * @return
 27      */
 28     @RequestMapping(value = "/get", method = RequestMethod.GET)
 29     public ModelAndView getByPrimaryKey(ModelAndView mav) {
 30         SysUser user = userService.getByPrimaryKey(38);
 31         mav.addObject("user", user);
 32         mav.setViewName("user");
 33         return mav;
 34     }
 35 
 36     /**
 37      * 获取用户列表
 38      * 
 39      * @param mav
 40      * @return
 41      */
 42     @RequestMapping(value = "/list", method = RequestMethod.GET)
 43     public ModelAndView listSysUser(ModelAndView mav) {
 44         List<SysUser> list = userService.listSysUser();
 45         mav.addObject("list", list);
 46         mav.setViewName("list");
 47         return mav;
 48     }
 49 
 50     /**
 51      * 跳转到新增用户页面
 52      * 
 53      * @param mav
 54      * @return
 55      */
 56     @RequestMapping(value = "/add", method = RequestMethod.GET)
 57     public ModelAndView addSysUser(ModelAndView mav) {
 58         mav.setViewName("add");
 59         return mav;
 60     }
 61 
 62     /**
 63      * 处理新增用户请求
 64      * 
 65      * @param mav
 66      * @param user
 67      * @return
 68      */
 69     @RequestMapping(value = "/insert", method = RequestMethod.POST)
 70     public String insertSysUser(@RequestParam("loginName") String loginName, @RequestParam("name") String name, @RequestParam("password") String password) {
 71         SysUser user = new SysUser();
 72         user.setLoginName(loginName);
 73         user.setPassword(password);
 74         user.setName(name);
 75         userService.insertSysUser(user);
 76         return "redirect:/user/list";  
 77     }
 78 
 79     /**
 80      * 处理删除用户请求
 81      * 
 82      * @param mav
 83      * @param id
 84      * @return
 85      */
 86     @RequestMapping(value = "/delete", method = RequestMethod.GET)
 87     public String deleteByPrimaryKey(@RequestParam("id") Integer id) {
 88         int count = userService.deleteByPrimaryKey(id);
 89         if (count == 1) {
 90             System.out.println("删除成功!");
 91         }else{
 92             System.out.println("删除失败!");
 93         }
 94         return "redirect:/user/list";  
 95     }
 96 
 97     /**
 98      * 跳转到修改用户页面
 99      * 
100      * @param mav
101      * @param user
102      * @return
103      */
104     @RequestMapping(value = "/modify", method = RequestMethod.GET)
105     public String modifyByPrimaryKey(Model model, @RequestParam("id") Integer id) {
106         SysUser user = userService.getByPrimaryKey(id);
107         model.addAttribute("user", user);
108         return "/modify";
109     }
110 
111     /**
112      * 处理修改用户请求
113      * 
114      * @param mav
115      * @param user
116      * @return
117      */
118     @RequestMapping(value = "/update", method = RequestMethod.POST)
119     public String updateByPrimaryKey(@RequestParam("id") Integer id, @RequestParam("loginName") String loginName, @RequestParam("name") String name) {
120         SysUser user = userService.getByPrimaryKey(id);
121         user.setLoginName(loginName);
122         user.setName(name);
123         userService.updateByPrimaryKey(user);
124         return "redirect:/user/list";  
125     }
126 }

com.elanbase.hr.config 配置类包

DruidConfig.java

 1 package com.elanbase.hr.config;
 2 
 3 import java.util.Arrays;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import javax.sql.DataSource;
 8 
 9 import org.springframework.boot.context.properties.ConfigurationProperties;
10 import org.springframework.boot.web.servlet.FilterRegistrationBean;
11 import org.springframework.boot.web.servlet.ServletRegistrationBean;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
14 
15 import com.alibaba.druid.pool.DruidDataSource;
16 import com.alibaba.druid.support.http.StatViewServlet;
17 import com.alibaba.druid.support.http.WebStatFilter;
18 
19 @Configuration
20 public class DruidConfig {
21 
22     @ConfigurationProperties(prefix = "spring.datasource")
23     @Bean
24     public DataSource druid() {
25         return new DruidDataSource();
26     }
27 
28     // 配置Druid的监控
29     // 1、配置一个管理后台的Servlet
30     @Bean
31     public ServletRegistrationBean statViewServlet() {
32         ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
33         Map<String, String> initParams = new HashMap<>();
34 
35         initParams.put("username", "admin");
36         initParams.put("password", "123456");
37         initParams.put("allow", "");// 默认就是允许所有访问
38         initParams.put("deny", "192.168.15.21");
39 
40         bean.setInitParameters(initParams);
41         return bean;
42     }
43 
44     // 2、配置一个web监控的filter
45     @Bean
46     public FilterRegistrationBean webStatFilter() {
47         FilterRegistrationBean bean = new FilterRegistrationBean();
48         bean.setFilter(new WebStatFilter());
49 
50         Map<String, String> initParams = new HashMap<>();
51         initParams.put("exclusions", "*.js,*.css,/druid/*");
52 
53         bean.setInitParameters(initParams);
54 
55         bean.setUrlPatterns(Arrays.asList("/*"));
56 
57         return bean;
58     }
59 }

com.elanbase.hr.common 公用类包

 6、页面:基本CRUD操作

list.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>list</title>
 6 </head>
 7 <body>
 8     <h1>新增用户</h1>
 9     <form action="insert" method="POST">
10         登录名:<input type="text" id="loginName" name="loginName" /> <br>
11         用户名:<input type="text" id="name" name="name" /> <br>
12         密码:<input type="text" id="password" name="password" /> <br>
13         <input type="submit" value="提交" />
14     </form>
15     
16     <h1>用户列表</h1>
17     <table border="1" style="width: 500px; height: 70px;">
18         <tr>
19             <td>ID</td>
20             <td>登录名</td>
21             <td>用户名</td>
22             <td>密码</td>
23             <td>操作</td>
24         </tr>
25 
26         <#list list as user>
27         <tr>
28             <td>${user.id}</td>
29             <td>${user.loginName}</td>
30             <td>${user.name}</td>
31             <td>${user.password}</td>
32             <td>
33                 <a href="delete?id=${user.id}">删除</a> | <a href="modify?id=${user.id}">修改</a>
34             </td>
35         </tr>
36         </#list>
37     </table>
38 
39 </body>
40 </html>

modify.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>list</title>
 6 </head>
 7 <body>
 8     <h1>修改用户</h1>
 9     <form action="../user/update" method="POST">
10         用户ID:<input type="text" id="id" name="id" value="${user.id}" readonly="readonly" /> <br>
11         登录名:<input type="text" id="loginName" name="loginName" value="${user.loginName}" /> <br>
12         用户名:<input type="text" id="name" name="name" value="${user.name}" /> <br>
13         <input type="submit" value="提交" />
14         
15     </form>
16     
17 </body>
18 </html>

http://localhost:8080/elanbasehr/user/list

技术分享图片

 

SSM框架整合

标签:引擎   eth   ted   index.jsp   生成   console   删除用户   hone   实体映射   

原文地址:https://www.cnblogs.com/java123456/p/8949501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!