整合SSM框架,实现用户登录和图书的增删改查
下面为效果图
实现思想
- 搭建好项目结构,添加Web支持
- 搭建数据库以及对应的实体类
- 从最底层开始写Dao-Service-ServiceImpl-Controller
- 编写jsp页面,实现数据的增删改查
项目结构
从最底层Dao层开始写代码,创建一个Dao包,里面的是接口
AccountDao
//不需要写注解,他会自动注解
public interface AccountDao {
//验证登录操作
List<Account> findAll(Account account);
}
编写Mybatis配置文件,执行查询操作
AccountDao.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">
<!--namespace的值习惯上设置成包名+sql映射文件名,
这样就能够保证namespace的值是唯一的-->
<mapper namespace="com.dao.AccountDao">
<select id="findAll" parameterType="com.entity.Account" resultType="com.entity.Account">
select * from accounttab where accountName=#{accountName} and accountPassword=#{accountPassword}
</select>
</mapper>
3.编写实体类
Account类
package com.entity;
/**
* @author 爱敲代码的小白
* @date 2020/12/8 9:22
*/
public class Account {
private Integer accountId;//用户ID
private String accountName;//用户名
private String accountPassword;//用户密码
public Account() {
}
public Account(Integer accountId, String accountName, String accountPassword) {
this.accountId = accountId;
this.accountName = accountName;
this.accountPassword = accountPassword;
}
public Integer getAccountId() {
return accountId;
}
public void setAccountId(Integer accountId) {
this.accountId = accountId;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountPassword() {
return accountPassword;
}
public void setAccountPassword(String accountPassword) {
this.accountPassword = accountPassword;
}
@Override
public String toString() {
return "Account{" +
"accountId=" + accountId +
", accountName='" + accountName + '\'' +
", accountPassword='" + accountPassword + '\'' +
'}';
}
}
4.编写Service(服务层)
AccountService
public interface AccountService {
List<Account> findAll(Account account);
}
编写实现类AccountServiceImpl
package com.service.impl;
import com.dao.AccountDao;
import com.entity.Account;
import com.entity.Book;
import com.service.AccountService;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author 爱敲代码的小白
* @date 2020/12/8 9:29
*/
@Service("accountService")//速度快一些
public class AccountServiceImpl implements AccountService {
/*@Autowired //自动注入
private AccountDao accountDao;*/
final AccountDao accountDao;
//推荐构造器注入
public AccountServiceImpl(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public List<Account> findAll(Account account) {
//调用dao层的方法
List<Account> list = accountDao.findAll(account);
//三元运算符,list.size不为空返回list,为空返回null
return list.size()!=0?list:null;
}
}
采用构造器的方式实现Dao和Service层依赖注入,
5.编写控制层Controller
AccountController
package com.controller;
import com.entity.Account;
import com.entity.Book;
import com.service.AccountService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 爱敲代码的小白
* @date 2020/12/8 9:36
*/
//用来处理用户的业务
@Controller
public class AccountController {
@Resource
AccountService accountService; //对应的是服务层的名称
@RequestMapping("login") //都支持提交方式
//跳转用model
public String login(Account at, Model model){
List<Account> all = accountService.findAll(at);
//登录成功
if(all!=null){
return "redirect:main";//替换掉,定向于bookController处理业务,登录成功则进入图书界面
}else {
model.addAttribute("errorMsg","账户名密码错误,请重新输入");
return "login"; //将用户登录错误信息返回到login.jspp页面
}
}
}
6.编写jsp文件
login.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020-12-04
Time: 上午 8:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form method="post" action="login"> <%--将表单信息提交到控制层处理--%>
<table>
<tr><td></td><td>${errorMsg}</td></tr> <%--将错误信息返回到本页面接收,一般不会这样,会在新建一个error.jsp页面接收--%>
<tr><td>用户名:</td>
<td><input type="text" name="accountName"/></td></tr>
<tr><td>密码:</td><td>
<input type="password" name="accountPassword"/>
</td></tr>
<tr><td> <input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td></tr>
</table>
</form>
</body>
</html>
接下来就是最重要的编写配置文件啦
我们这里采用的是Model2的形式来存放配置文件
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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">
<!-- 扫描指定的包和包下面指定的注释类型,分开扫描可以实现事务 -->
<context:component-scan base-package="com.service.impl">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}" />
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!--最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}" />
<!--最大空闲连接 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!--初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 配置MyBatis工厂SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!--指定核MyBatis心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="transactionManager" class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
db.properties(配置的是数据库连接方面)
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
dispatcer-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 加载相关Bean处理比如json -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- 扫描所有的controller 但是不扫描service-->
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!--视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--设置前缀,即为jsp文件所在的位置-->
<property name="prefix" value="/jsp/" />
<!--设置后缀-->
<property name="suffix" value=".jsp" />
</bean>
</beans>
Mybatis-config.xml
<?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>
<!-- 加载Mapper映射文件 -->
<mappers>
<!-- 空着就行,由spring配置加载 -->
</mappers>
</configuration>
web.xml(加载配置文件在里面执行)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>big6</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--使用Spring提供的编码过滤器来统一编码-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--拦截前端页面1中的所有请求,并交给CharacterEncodingFilter编码过滤器类来处理-->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端过滤器-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化加载配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<!--表示容器在启动时立即加载配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!--'/'会将所有的URL拦截,并交给DispatcherServlet处理-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 定义欢迎界面。默认是index.jsp-->
<welcome-file-list>
<welcome-file>/jsp/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
最激动的时候它来了,那就是运行一下
配置好Tomcat,将项目加载到Tomcat里面
登录成功,显示图书界面main.jsp
总结
- 数据库有俩张表,分别是accounttab(用户表)和bookinfo(图书信息表)
- 配置文件采用的是Model2形式,将配置文件放在src同级目录下