初学ssm框架(IDEA+Maven)项目练习
功能:登录,查询,修改,分页显示数据
资料链接(前端页面,配置文件): https://pan.baidu.com/s/1eam852Ba8yK8mgcUehAqQg
提取码: qeuh
工具:
Idea
MySQL 5.6
SQLYog
JDK 1.8
Tomcat8
Maven 3.6.0
Editplus
项目界面
项目结构目录
创建数据库 -db_gm 创建表 -tb_user
导入项目成功后开始配置
编写pom.xml,导入相应的jar包
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongruan</groupId>
<artifactId>SSM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssmdemo10 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- spring版本号 -->
<spring.version>5.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<c3p0.version>0.9.5.2</c3p0.version>
<taglibs.version>1.1.2</taglibs.version>
</properties>
<dependencies>
<!-- spring核心包 -->
<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-aop</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 数据连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${taglibs.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>
<!-- 导入servlet-api/jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- 分页架包 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>ssmdemo10</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
配置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、spring和mybatis整合-->
<!--引入配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源-->
<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>
<!--配置sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--扫描bean层包 使用别名-->
<property name="typeAliasesPackage" value="com.zhongruan.bean"/>
<!--配置加载映射文件 *mapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--调用了pagehelper分页插件,分页配置-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--自动生成dao,mapper-->
<!--配置扫描dao接口包,将dao层注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置要扫描的dao层路径-->
<property name="basePackage" value="com.zhongruan.dao"/>
<!--注入Spring容器中-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描全部包-->
<context:component-scan base-package="com.zhongruan"/>
<!--配置事物注解-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事物注解-->
<tx:annotation-driven/>
</beans>
配置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: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、注解扫描位置、注解支持-->
<context:component-scan base-package="com.zhongruan.controller"/>
<!--<mvc:annotation-driven/>可以代替下面两个-->
<!--2、配置映射处理器和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--3、视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--设置开头结尾-->
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置加载类路径文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!--配置解决中文乱码-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器,指定加载spring-mvc.xml-->
<servlet>
<servlet-name>dispatcherServlet</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>
<!--让服务器启动时就进行创建servlet对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--
/* 会把所有客户端请求都当做requestMapping指定路径取处理
/ 页面不会去拦截
*.do 为了区分
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
配置tomcat后启动项目
一、项目登录界面
分析
- 向数据库插入数据
- 输入用户名和密码后点击登录,将数据传到后端
- 后端查询数据库,将结果返回到前端页面
步骤:
在bean文件夹中新建一个实体类,与数据库中的数据对应
在dao文件夹中新建一个IUserInfoDao接口
在IUserInfoDao接口中新建一个doLogin方法
doLogin方法是根据用户名进行查询,并返回对象
在mapper文件夹中新建UserInfoMapper.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.IUserInfoDao">
<!--登录-->
<!--输入参数类型是字符串,返回类型是UserInfo实体类-->
<select id="doLogin" parameterType="String" resultType="com.zhongruan.bean.UserInfo">
select * from tb_user where username = #{username}
</select>
</mapper>
接着是在service层中新建IUserInfoService接口
在service层中新建一个Impl文件夹用来放实现方法(类上注解@Service)
在Impl文件夹中新建一个UserInfoServiceImp类,实现IUserInfoService接口
调用IUserInfoDao中的方法
接着到了controller层,在controller中新建UserController类
UserController类中创建doLogin方法,调用IUserInfoService接口中方法
方法中的参数名称对应jsp页面中的name值(username,password)
session存储信息提示用户(密码错误,用户名不存在等)
controller将根据数据进行页面跳转
前端login.jsp页面也要进行一些修改
提示信息对应session中存储的msg值
登录成功后跳转到main界面
其他的功能也是类似的步骤dao->service->controller
主要的代码
UserInfo实体类代码
package com.zhongruan.bean;
import javax.persistence.Entity;
/**
* date:2020-02-19
* author:
*/
@Entity
public class UserInfo {
//
//spring容器,去访问对象成员属性的时候必须是封装类型的,不然没法访问
private int id;
private String username;
private String password;
public UserInfo() {
}
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = 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 + '\'' +
'}';
}
}
dao层的IUserInfoDao代码
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import org.apache.ibatis.annotations.Param;
import java.util.HashMap;
import java.util.List;
/**
* date:2020-02-19
* author:
*/
//数据库访问层,可以直接访问数据库
public interface IUserInfoDao {
//验证登录
//查询数据库,select
UserInfo doLogin(String username);
//全部查询
List<UserInfo> findAll();
//增加
void addUser(UserInfo userInfo);
//删除,根据id
void delUserById(Integer id);
//分页查询
List<UserInfo> findByPage(@Param(value = "start") int start,@Param(value = "pageSize") int pageSize);
//查询记录数
int findTotalCount();
//修改前的查询
UserInfo updSelUserInfoById(int id);
//修改
void updUserInfo(UserInfo userInfo);
}
mapper中UserInfoMapper.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.IUserInfoDao">
<!--登录-->
<!---->
<select id="doLogin" parameterType="String" resultType="com.zhongruan.bean.UserInfo">
select * from tb_user where username = #{username}
</select>
<!--全部查询-->
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from tb_user
</select>
<!--增加-->
<insert id="addUser" parameterType="com.zhongruan.bean.UserInfo">
insert into tb_user(id,username,password) values (#{id},#{username},#{password})
</insert>
<!--删除-->
<delete id="delUserById" parameterType="Integer">
delete from tb_user where id =#{id}
</delete>
<!--修改前的查询-->
<select id="updSelUserInfoById" parameterType="Integer" resultType="com.zhongruan.bean.UserInfo">
select * from tb_user where id =#{id}
</select>
<!--更新-->
<update id="updUserInfo" parameterType="com.zhongruan.bean.UserInfo">
update tb_user set id = #{id},username = #{username},password = #{password} where id=#{id};
</update>
</mapper>
service层IUserInfoService层代码
package com.zhongruan.service;
import com.zhongruan.bean.PageBean;
import com.zhongruan.bean.UserInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* date:2020-02-19
* author:
*/
public interface IUserInfoService {
//登录
UserInfo doLogin(String username);
//分页查询
List<UserInfo> findAll(Integer page,Integer size);
//增加
void addUser(UserInfo userInfo);
//删除,根据id
void delUserById(Integer id);
//修改前的查询
UserInfo updSelUserInfoById(int id);
//修改
void updUserInfo(UserInfo userInfo);
}
service层中impl中UserInfoServiceImpl中代码
package com.zhongruan.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongruan.bean.PageBean;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserInfoDao;
import com.zhongruan.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
/**
* date:2020-02-19
* author:
*/
@Service
public class UserInfoServiceImpl implements IUserInfoService {
@Autowired
private IUserInfoDao UserInfoDao;
//登录
@Override
public UserInfo doLogin(String username) {
UserInfo userInfo = UserInfoDao.doLogin(username);
return userInfo;
}
//分页查询
@Override
public List<UserInfo> findAll(Integer page,Integer size) {
PageHelper.startPage(page, size);
return UserInfoDao.findAll();
}
//增加
@Override
public void addUser(UserInfo userInfo) {
UserInfoDao.addUser(userInfo);
}
//删除
@Override
public void delUserById(Integer id) {
UserInfoDao.delUserById(id);
}
//更新前查询
@Override
public UserInfo updSelUserInfoById(int id) {
return UserInfoDao.updSelUserInfoById(id);
}
//更新
@Override
public void updUserInfo(UserInfo userInfo) {
UserInfoDao.updUserInfo(userInfo);
}
}
controller层中UserController中代码
package com.zhongruan.controller;
import com.github.pagehelper.PageInfo;
import com.zhongruan.bean.PageBean;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserInfoService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.registry.infomodel.User;
import java.util.List;
/**
* date:2020-02-19
* author:
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserInfoService iUserInfoService;
@RequestMapping("/doLogin.do")
public String doLogin(String username, String password,HttpSession session) {
UserInfo userInfo = iUserInfoService.doLogin(username);
String msg="";
if (userInfo != null) {
if (userInfo.getPassword().equals(password)) {
System.out.println("登录成功!");
session.setAttribute("userInfo",userInfo);
return "main";
} else {
System.out.println("密码错误");
msg="密码错误";
session.setAttribute("msg",msg);
return "../login";
}
} else {
System.out.println("当前用户不存在");
msg="当前用户不存在";
session.setAttribute("msg",msg);
return "../login";
}
}
// 全部查询
// 所有需要返回数据到页面的全部都是要封装到ModelAndView中
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer size){
// 调用service中的方法得到查询方法
List<UserInfo> userInfoList = iUserInfoService.findAll(page,size);
PageInfo pageInfo=new PageInfo(userInfoList);
ModelAndView mv=new ModelAndView();
mv.addObject("pageInfo",pageInfo);
// 设置显示页面
mv.setViewName("user-list");
return mv;
}
//添加
@RequestMapping("/addUser.do")
public String addUser(UserInfo userInfo){
iUserInfoService.addUser(userInfo);
// 增加成功,返回重新查询
return "redirect:findAll.do";
}
// 删除
@RequestMapping("/delUser.do")
public String delUserById(int id){
iUserInfoService.delUserById(id);
System.out.println("删除成功!");
return "redirect:findAll.do";
}
// 修改前的查询
@RequestMapping("/updUserInfoById.do")
public ModelAndView updUserById(int id){
UserInfo userInfo = iUserInfoService.updSelUserInfoById(id);
ModelAndView mv=new ModelAndView();
mv.addObject("userInfo",userInfo);
mv.setViewName("user-update");
return mv;
}
// 修改数据
@RequestMapping("updUserInfo.do")
public String updUserInfo(UserInfo userInfo){
iUserInfoService.updUserInfo(userInfo);
System.out.println("修改数据成功!");
return "redirect:findAll.do";
}
}
修改后的前端jsp页面链接: https://pan.baidu.com/s/1HLT15866wilwcFeU-A1oTA 提取码: hhi1
完整的(会多出来几个方法,实验用的,自动忽略):
链接: https://pan.baidu.com/s/1Hl1RRQRq6TnSrcrvpBGXfQ
提取码: xgx8
来源:CSDN
作者:刚学的小白
链接:https://blog.csdn.net/z7kirictol/article/details/103480937