A long time ago SSM Framework to write a registration login small program ( Of course, the front end uses templates ), It can be regarded as one's own entry-level work . A little change tonight , Take it out for everyone to learn , The program may have BUG, Please forgive me .

Let's talk about my running environment :

*
Myeclipse 2017 CI 1

*
MySQL 5.7

* Tomcat 8.5
The project structure is as follows



 

 

 

New in database user surface :



There are several major configuration files , Take a look at the role of each profile .

1. web.xml: When the service starts, it will load first web.xml This resource file , It includes the front-end controller , Random code problem and other configuration .

2.applicatonContext.xml : General configuration data source , thing , annotation etc. .

I'm using it here applicatonContext-*.xml The form of DAO layer ,Service layer ,Transaction Layer separation configuration , This is easy to manage

Respectively applicatonContext-dao.xml,applicatonContext-service.xml,
applicatonContext-transaction.xml

Separate configuration , Need to be in web.xml Configuration context location in

3.springmvc.xml: It is configured in the control layer , Such as view parser static resources , mvc File upload , Interceptors, etc .

4.SqlMapConfig.xml: The configuration file is MyBatis Configuration file for , There is no need to configure it , Everything to spring Administration , however xml Basic file configuration .

    Persistence layer related configuration files applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- to configure
read properties file jdbc.properties --> <context:property-placeholder
location="classpath:jdbc.properties" /> <!-- to configure data source --> <bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource"> <property
name="driverClassName" value="${jdbc.driver}" /> <property name="url"
value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> </bean> <!--
to configure SqlSessionFactory --> <bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- set up MyBatis Core profile --> <property name="configLocation"
value="classpath:SqlMapConfig.xml" /> <!-- set up data sources --> <property
name="dataSource" ref="dataSource" /> <!-- Alias package scan --> <property
name="typeAliasesPackage" value="com.zhu.pojo" /> </bean> <!-- to configure Mapper scanning -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--
set up Mapper Scanning package --> <property name="basePackage" value="com.zhu.mapper" /> </bean>
</beans>
    applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--
to configure @Service Package scan of class --> <context:component-scan base-package="com.zhu.service"/>
</beans>
  applicationContext-trans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- Transaction manager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--
data source --> <property name="dataSource" ref="dataSource" /> </bean> <!-- notice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes> <!-- Communication behavior --> <tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*"
propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*"
propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS"
read-only="true" /> <tx:method name="select*" propagation="SUPPORTS"
read-only="true" /> <tx:method name="get*" propagation="SUPPORTS"
read-only="true" /> <tx:method name="query*" propagation="SUPPORTS"
read-only="true" /> </tx:attributes> </tx:advice> <!-- section --> <aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(*
com.zhu.service.*.*(..))" /> </aop:config> </beans>
springmvc.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--
to configure Controller scanning --> <context:component-scan base-package="com.zhu.controller" />
<!-- Configure annotation driven --> <mvc:annotation-driven /> <!-- Configure view parser --> <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--
prefix --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- suffix --> <property
name="suffix" value=".jsp" /> </bean> </beans>
  SqlMapConfig.xml This does not need to be set , But it has to be
<?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> </configuration>
The configuration file is written , And then you start writing code

First from Mapper Start writing :

* Write an interface first UserMapper.java package com.zhu.mapper; import com.zhu.pojo.User;
public interface UserMapper { // validate logon User logincheck(User user); // register void
register(User user); }
    2.            Writing a UserMapper.xml Associated with it
<?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.zhu.mapper.UserMapper"> <select id="logincheck"
parameterType="com.zhu.pojo.User" resultType="com.zhu.pojo.User"> select * from
user <where> <if test="name != null and name != ''"> and `name` = #{name} </if>
<if test="pwd != null and pwd != ''"> AND `pwd` = #{pwd} </if> </where>
</select> <insert id="register" parameterType="com.zhu.pojo.User"> insert into
user(name,pwd) value(#{name},#{pwd}) </insert> </mapper>
    3. The next thing is Service Layer , Write an interface first :UserService.java, Content and UserMapper.java equally

    4. Then write a class implementation UserService.java, The code is relatively simple , Don't explain too much , Don't forget to mark it @Service
package com.zhu.service; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.stereotype.Service; import com.zhu.mapper.UserMapper;
import com.zhu.pojo.User; @Service public class UserServiceImpl implements
UserService { // Attribute injection @Autowired private UserMapper usermapper; @Override public
User logincheck(User user) { User u = usermapper.logincheck(user); return u; }
@Override public void register(User user) { usermapper.register(user); } }
   5. In the end Controller Layer ,Controller Layer code is mainly called Service Layer implementation method , Implement some operations , And interact with the front end .

      UserController.java
package com.zhu.controller; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.stereotype.Controller; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import
org.springframework.web.bind.annotation.RequestParam; import com.zhu.pojo.User;
import com.zhu.service.UserService; @Controller public class UserController {
@Autowired private UserService userService; // Turn to login page @RequestMapping("tologin")
public String tologin(){ return "login"; } // validate logon @RequestMapping("login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,Model model){ User user = new User();
user.setName(username); user.setPwd(password); if(userService.logincheck(user)
!= null){ model.addAttribute("username",username); return "index"; } else{
model.addAttribute("error"," Wrong account or password "); return "login"; } } // Turn to registration page
@RequestMapping("toregister") public String toregister(){ return "register"; }
// register @RequestMapping("register") public String
register(@RequestParam("username") String username, @RequestParam("password")
String password){ User user = new User(); user.setName(username);
user.setPwd(password); userService.register(user); return "login"; } }
Code link :https://download.csdn.net/download/qq_22774179/10675002
<https://download.csdn.net/download/qq_22774179/10675002>

Effect demonstration :http://gikhg574109.jsp.jspee.com.cn/LoginAndRegister/tologin.action
<http://gikhg574109.jsp.jspee.com.cn/LoginAndRegister/tologin.action>

( Write for the second time , It's not very good , Please forgive me !)