Message: This paper uses IDEA to develop, and suggests to build the project first according to the article order. As long as the directory structure is built first, it is the problem of copy and paste. Inside the code also has comments, first build up, slowly looking at the code thinking, want to ask what to do? Even if you are a beginner, you can build integration step by step.
Step directory | |
---|---|
1. Create Maven Project | |
1.1-1.6 View Operation | |
2. Configuration files | |
2.1pom.xml | |
2.2jdbc.properties | |
2.3log4j.properties | |
2.4spring-mvc.xml | |
2.5spring-mybatis.xml | |
2.6web.xml | |
3. create demo | |
3.1Controller | |
3.2 Dao | |
3.3 model | |
3.3.1 Database Files | |
3.4 Service | |
3.4.1 ServiceImpl | |
4. Project deployment | |
4.1 Identification Catalogue | |
4.3 Create a Tomca | |
4.4 Configure access root path | |
4.5 Browser Access |
1. Create maven projects
1.1 File - > New Module, enter the Create Project window.
1.2 Click Next and fill in GroupId, ArtifactId and Version
1.3 Next, you need to add a parameter archetype Catalog = internal to Properties.
1.4 Fill in Module name
Create completion
Build the project skeleton: Maven downloads the jar package needed automatically. The following code snippet can be skipped to output Maven's log for the console
C:\Develop\jdk1.8.0_25\bin\java -Dmaven.multiModuleProjectDirectory=C:\Users\NINTH\AppData\Local\Temp\archetypetmp -Dmaven.home=C:\Develop\IDEA_2017.2.5\plugins\maven\lib\maven3 -Dclassworlds.conf=C:\Develop\IDEA_2017.2.5\plugins\maven\lib\maven3\bin\m2.conf -Dfile.encoding=UTF-8 -classpath C:\Develop\IDEA_2017.2.5\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.2.5 -DinteractiveMode=false -DgroupId=com.ninth.web -DartifactId=web-ssm -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=RELEASE org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate [INFO] Scanning for projects... Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (825 B at 0.2 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.pom (10 KB at 21.2 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype/3.0.1/maven-archetype-3.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype/3.0.1/maven-archetype-3.0.1.pom (13 KB at 20.8 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/30/maven-parent-30.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/30/maven-parent-30.pom (41 KB at 52.7 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/18/apache-18.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/18/apache-18.pom (16 KB at 30.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.jar (94 KB at 82.6 KB/sec) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom --- Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/3.0.1/archetype-catalog-3.0.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/3.0.1/archetype-catalog-3.0.1.pom (2 KB at 4.6 KB/sec) ....................A large part of the middle is omitted......................... [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: C:\Users\NINTH\AppData\Local\Temp\archetypetmp [INFO] Parameter: package, Value: com.ninth.web [INFO] Parameter: groupId, Value: com.ninth.web [INFO] Parameter: artifactId, Value: web-ssm [INFO] Parameter: packageName, Value: com.ninth.web [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:\Users\NINTH\AppData\Local\Temp\archetypetmp\web-ssm [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:29 min [INFO] Finished at: 2017-10-27T16:41:21+08:00 [INFO] Final Memory: 15M/287M [INFO] ------------------------------------------------------------------------ [INFO] Maven execution finished
1.6 After the Maven skeleton has been built, I suggest that all the files needed by the project can be prepared according to the following catalog diagram. It is very simple and easy to copy the code to each file.
2. Project configuration
2.1 Project Profile pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.heitian.web</groupId> <artifactId>web-ssm</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>web-ssm Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- spring version number --> <spring.version>4.2.5.RELEASE</spring.version> <!-- mybatis version number --> <mybatis.version>3.2.8</mybatis.version> <!-- mysql Driver version number --> <mysql-driver.version>5.1.29</mysql-driver.version> <!-- log4j Log Pack Version Number --> <slf4j.version>1.7.18</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- Add to jstl rely on --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- Add to junit4 rely on --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- Specify ranges, which are loaded only during testing --> <scope>test</scope> </dependency> <!-- Add to spring Core dependence --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- Add to mybatis rely on --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Add to mybatis/spring Integration package dependency --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- Add to mysql Driver dependency --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!-- Adding database connection pool dependencies --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- Add to fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <!-- Add log correlation jar package --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- Reflect JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> </dependencies> <build> <finalName>web-ssm</finalName> </build> </project>
2.2 Configure the JDBC file jdbc.properties
driverClasss=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull username=root password=root #Define the number of initial connections initialSize=0 #Define the maximum number of connections maxActive=20 #Define maximum idleness maxIdle=20 #Define minimum idleness minIdle=1 #Define the longest waiting time maxWait=60000
2.3 log4j configuration file log4j.properties
log4j.rootLogger=INFO,Console,File #console log log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n #Ordinary File Log log4j.appender.File=org.apache.log4j.RollingFileAppender log4j.appender.File.File=logs/ssm.log log4j.appender.File.MaxFileSize=10MB #Output logs, if replaced by DEBUG, indicate the output of logs above DEBUG level log4j.appender.File.Threshold=ALL log4j.appender.File.layout=org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
2.4 Configure spring MVC file spring-mvc.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- Automatic scanning @Controller--> <context:component-scan base-package="com.heitian.ssm.controller"/> <!--avoid IE implement AJAX Return when JSON Download file appears --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- start-up SpringMVC Annotation function to complete requests and annotations POJO Mapping --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON Converter --> </list> </property> </bean> <!-- Define the prefix and suffix of the jumped file, view mode configuration --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"/> </bean> <!-- File Upload Configuration --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- Default encoding --> <property name="defaultEncoding" value="UTF-8"/> <!-- Upload file size limit is 31 M,31*1024*1024 --> <property name="maxUploadSize" value="32505856"/> <!-- Maximum in memory --> <property name="maxInMemorySize" value="4096"/> </bean> </beans>
2.5 Configure the configuration file spring-mybatis.xml needed for spring and mybatis integration
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Automatic scanning --> <context:component-scan base-package="com.heitian.ssm"/> <!-- The first way is to load one properties file --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <!-- The second way: Loading multiple properties file <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:common.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean> --> <!-- Configuring data sources --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClasss}"/> <property name="url" value="${jdbcUrl}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <!-- Initialize connection size --> <property name="initialSize" value="${initialSize}"></property> <!-- Maximum number of connection pools --> <property name="maxActive" value="${maxActive}"></property> <!-- Maximum idleness of connection pool --> <property name="maxIdle" value="${maxIdle}"></property> <!-- Connection pool minimum idleness --> <property name="minIdle" value="${minIdle}"></property> <!-- Get the maximum connection waiting time --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- mybatis and spring Perfect integration, no need mybatis Configuration mapping file --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- Automatic scanning mapping.xml file --> <property name="mapperLocations" value="classpath:mapping/*.xml"></property> </bean> <!-- DAO The package name of the interface, Spring The following classes are automatically found --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.heitian.ssm.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (transaction management)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- (transaction management)transaction manager, use JtaTransactionManager for global tx --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
2.6web.xml configuration file web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>web-ssm</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- Coding filter --> <filter> <filter-name>encodingFilter</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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring Monitor --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Prevent spring Memory overflow listeners, such as quartz --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring mvc servlet--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Here you can also configure it as *.do form --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- session To configure --> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
3. Make a query demo
3.1 Controller file UserController
package com.ninth.ssm.controller; import com.ninth.ssm.model.User; import com.ninth.ssm.service.UserService; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * Created by Ninth on 2017-10-28 15:29. * **/ @Controller @RequestMapping("/user") public class UserController { private Logger log = Logger.getLogger(UserController.class); @Resource private UserService userService; @RequestMapping("/showUser") public String showUser(HttpServletRequest request, Model model){ log.info("Query all user information"); List<User> userList = userService.getAllUser(); model.addAttribute("userList",userList); return "showUser"; } }
3.2 dao file UserDao
package com.ninth.ssm.dao; import com.ninth.ssm.model.User; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; /** * Created by Ninth on 2017-10-28 15:29. * **/ @Repository public interface UserDao { User selectUserById(@Param("userId") Long userId); User selectUserByPhoneOrEmail(@Param("emailOrPhone") String emailOrPhone, @Param("state") Short state); List<User> selectAllUser(); }
3.3 model file User
package com.ninth.ssm.model; import java.util.Date; /** * Created by Ninth on 2017-10-28 15:29. * **/ public class User { private Long id; private String userName; private String userPhone; private String userEmail; private String userPwd; private String pwdSalt; private Date createTime; private Date modifyTime; private Short isDelete; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPhone() { return userPhone; } public void setUserPhone(String userPhone) { this.userPhone = userPhone; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getPwdSalt() { return pwdSalt; } public void setPwdSalt(String pwdSalt) { this.pwdSalt = pwdSalt; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getModifyTime() { return modifyTime; } public void setModifyTime(Date modifyTime) { this.modifyTime = modifyTime; } public Short getIsDelete() { return isDelete; } public void setIsDelete(Short isDelete) { this.isDelete = isDelete; } }
3.3.1 Database Files
Create the database db_ssm in sql, then run the SQL file in the directory, automatically generate tables, of course, you can also create tables manually.
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `t_user` -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` bigint(11) NOT NULL auto_increment, `user_name` varchar(255) default NULL COMMENT 'User name', `user_phone` varchar(20) default NULL COMMENT 'Cell-phone number', `user_email` varchar(255) default NULL COMMENT 'E-mail address', `user_pwd` varchar(32) default NULL COMMENT 'User password after salt addition', `pwd_salt` varchar(6) default NULL COMMENT 'MD5 salt', `create_time` datetime default NULL COMMENT 'Creation time', `modify_time` datetime default NULL COMMENT 'Final modification time', `is_delete` tinyint(4) default NULL COMMENT 'Whether to delete, 0-Not deleted; 1-Deleted', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='User login table'; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES ('1', 'Da Bao Zhao', '13285250574', '1045221654@qq.com', '05126a423a9379d529e4ee61a212fa55', 'KJUYT5', '2016-07-15 23:38:56', '2016-07-15 23:39:09', '0'); INSERT INTO `t_user` VALUES ('2', 'Zhang Sanfeng', '15985250574', '1198224554@qq.com', '98bd3a1bebde01ad363d3c5a0d1e56da', '656JHU', '2016-07-15 23:39:01', '2016-07-15 23:39:13', '0'); INSERT INTO `t_user` VALUES ('3', 'Wang Ni Ma', '13685250574', '1256221654@qq.com', '5470db9b63c354f6c8d628b80ae2f3c3', '89UIKQ', '2016-07-15 23:39:05', '2016-07-15 23:39:16', '0');
3.4 Service file UserService
package com.ninth.ssm.service; import com.ninth.ssm.model.User; import java.util.List; /** * Created by Ninth on 2017-10-28 15:30. * **/ public interface UserService { List<User> getAllUser(); User getUserByPhoneOrEmail(String emailOrPhone, Short state); User getUserById(Long userId); }
3.4.1 Service Implementation Class IUserService impl
package com.ninth.ssm.service.impl; import com.ninth.ssm.dao.UserDao; import com.ninth.ssm.model.User; import com.ninth.ssm.service.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; /** * Created by Ninth on 2017-10-28 15:30. * **/ @Service @Transactional(rollbackFor = Exception.class) public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public User getUserById(Long userId) { return userDao.selectUserById(userId); } public User getUserByPhoneOrEmail(String emailOrPhone, Short state) { return userDao.selectUserByPhoneOrEmail(emailOrPhone,state); } public List<User> getAllUser() { return userDao.selectAllUser(); } }
3.5 Mapper file UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.ninth.ssm.dao.UserDao"> <resultMap id="UserBaseMap" type="com.ninth.ssm.model.User"> <id column="id" property="id" jdbcType="BIGINT"/> <result column="user_name" property="userName" jdbcType="VARCHAR"/> <result column="user_phone" property="userPhone" jdbcType="VARCHAR"/> <result column="user_email" property="userEmail" jdbcType="VARCHAR"/> <result column="user_pwd" property="userPwd" jdbcType="VARCHAR"/> <result column="pwd_salt" property="pwdSalt" jdbcType="VARCHAR"/> <result column="create_time" property="createTime" jdbcType="DATE"/> <result column="modify_time" property="modifyTime" jdbcType="DATE"/> <result column="is_delete" property="isDelete" jdbcType="SMALLINT"></result> </resultMap> <select id="selectUserById" parameterType="java.lang.Long" resultMap="UserBaseMap"> SELECT * FROM t_user WHERE id = #{userId} </select> <select id="selectUserByPhoneOrEmail" resultMap="UserBaseMap"> SELECT * FROM t_user WHERE user_email = #{emailOrPhone} OR user_phone = #{emailOrPhone} AND user_state = #{state} </select> <select id="selectAllUser" resultMap="UserBaseMap"> SELECT * FROM t_user </select> </mapper>