SSM framework realizes the addition, deletion, modification, query, paging and distribution of projects

Posted by bravo81 on Fri, 01 Nov 2019 21:51:09 +0100

1. Create a new project in idea, file - > New - > project. Select Maven, pay attention to jdk1.8, and click Next.

2.k here, Group,Artifactld, click Next.

3. Click finish.

4.ssm_parent1 is the parent class and does not write anything. Then four modules need to be created under ssm_parent1: ssm_bean,ssm_service,ssm_dao,ssm_web.
Then create packages, classes and interfaces under java of each Module. It should be said that there may not be java and resources under SSM web at first. You need to create your own Directory. Then right click to find Mark Directory as, and select Source Root and Resources Root respectively.

The direct images of Packge, Directory, File and so on are as follows:




6.userInfo:

package com.zhongruan.bean;

public class UserInfo {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

IUserDao:

package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;

import java.util.List;

public interface IUserDao {
    public UserInfo doLogin(UserInfo userInfo);
    public List<UserInfo> findAll();
    public  void delete(int id);
    public  void update(UserInfo userInfo);
    public void insert(UserInfo userInfo);
    public UserInfo findId(int id);

}

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.zhongruan.dao.IUserDao" >
    <select id="doLogin" parameterType="com.zhongruan.bean.UserInfo"
            resultType="com.zhongruan.bean.UserInfo">
        select * from userinfo where username=#{username} and password=#{password}
    </select>

    <select id="findAll" resultType="com.zhongruan.bean.UserInfo">
        select * from userinfo
    </select>

    <insert id="insert" parameterType="com.zhongruan.bean.UserInfo">
        insert into userinfo values (#{id},#{username},#{password})
    </insert>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from userinfo where id =#{id}
    </delete>

    <update id="update" parameterType="com.zhongruan.bean.UserInfo">
        update userinfo set username=#{username},password=#{password} where id=#{id}
    </update>

    <select id="findId" parameterType="java.lang.Integer" resultType="com.zhongruan.bean.UserInfo">
        select * from userinfo where id=#{id}
    </select>

</mapper>

UserService:

package com.zhongruan.service.impl;
import com.github.pagehelper.PageHelper;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    private IUserDao userDao;
    private int page;
    private int size;

    @Override
    public boolean doLogin(UserInfo userInfo) {
        UserInfo userInfo1 = userDao.doLogin(userInfo);
        if (null != userInfo1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<UserInfo> findAll(int page,int size) {
        PageHelper.startPage(page,size);
        return userDao.findAll();
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }
   @Override
    public void insert(UserInfo userInfo) {
        userDao.insert(userInfo);
    }
    @Override
    public void update(UserInfo userInfo) {
        userDao.update(userInfo);
    }
    @Override
    public UserInfo findId(int id) {
        return userDao.findId(id);

    }
}

IUserService:

package com.zhongruan.service;

import com.zhongruan.bean.UserInfo;

import javax.xml.registry.infomodel.User;
import java.util.List;

public interface IUserService {
    public boolean doLogin(UserInfo userInfo);

    public List<UserInfo> findAll(int page,int size);

    public void update(UserInfo userinfo);

    public UserInfo findId(int id);

    public void delete(int id);

    public void insert(UserInfo userInfo);
}

UserController:

package com.zhongruan.controller;

import com.github.pagehelper.PageInfo;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    private int page;
    private int size;

    @RequestMapping("/login.do")
    public ModelAndView login(UserInfo userInfo){
        ModelAndView mv=new ModelAndView();
        boolean flag=userService.doLogin(userInfo);
        if(flag){
            mv.setViewName("main");
        }else{
            mv.setViewName("../failer");
        }
        return mv;
    }
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(int page,int size)
    {
        List<UserInfo> all=userService.findAll(page,size);
        ModelAndView mv=new ModelAndView();
        PageInfo<UserInfo> pageInfos=new PageInfo<>(all);
        mv.addObject("pageInfo",pageInfos);
        mv.setViewName("user-list");
        return mv;
    }

    @RequestMapping("/save.do")
    public ModelAndView save(UserInfo userinfo) {
        userService.insert(userinfo);
        ModelAndView mv = new ModelAndView();
        mv = findAll(1,5);
        return mv;
    }

    @RequestMapping("/delete.do")
    public ModelAndView delete(int id) {
        userService.delete(id);
        ModelAndView mv=new ModelAndView();
        mv=findAll(1,5);
        return mv;

    }
    @RequestMapping("/toUpdate.do")
    public ModelAndView toUpdate(int id) {
        UserInfo userinfo = userService.findId(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("user",userinfo);
        mv.setViewName("user-update");
        return mv;

    }

    @RequestMapping("/update.do")
    public ModelAndView update(UserInfo userinfo) {
        userService.update(userinfo);
        ModelAndView mv = new ModelAndView();
        mv = findAll(1,5);
        return mv;
    }


}


applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 1.Configuration database related parameters properties Properties: ${url} -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 2.Configure data sources -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <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="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!-- 3.To configure SqlSessionFactory object -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- Inject database connection pool -->
        <property name="dataSource" ref="dataSource"/>
        <!-- scanning bean Package use alias -->
        <property name="typeAliasesPackage" value="com.zhongruan.bean"></property>

        <!--Configure load mapping file UserMapper.xml-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="resonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>


    </bean>

    <!-- Automatic generation dao,mapper-->
    <!-- 4.Configuration scan Dao Interface package, dynamic implementation Dao Interface, injecting into spring Container -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- Give the need to scan Dao Interface package -->
        <property name="basePackage" value="com.zhongruan.dao"/>
        <!-- injection sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>



    <!--Automatic scanning-->
    <context:component-scan base-package="com.zhongruan"/>


    <!-- Configuration transaction-->
    <!-- 5.Configure transaction manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 6.Enable transaction annotation-->
    <tx:annotation-driven></tx:annotation-driven>

</beans>

db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

spring-mvc.xml:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 1.Annotation scan location-->
    <context:component-scan base-package="com.zhongruan.controller" />

    <!-- 2.Configure map processing and adapters-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <!-- 3.View parser-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Operation result:

Topics: log4j Spring JDBC xml