昨天的ssm框架已经有了基本的雏形,但还不是很完善,我打算直接奉上我的文件夹,然后来解析如何实现增删改查
替换原properties以及dependencies
替换完成后需要一段时间下载,如果按住Ctrl键,鼠标dependencies中的任意一项,出现跳转即为下载、配置完成。(效果如图)
<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.0modelVersion>
<groupId>com.mydaygroupId>
<artifactId>ssmday0628artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>ssmday0628 Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<spring.version>5.0.2.RELEASEspring.version>
<mybatis.version>3.2.6mybatis.version>
<slf4j.version>1.7.7slf4j.version>
<log4j.version>1.2.17log4j.version>
<c3p0.version>0.9.5.2c3p0.version>
<taglibs.version>1.1.2taglibs.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-oxmartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>javaxgroupId>
<artifactId>javaee-apiartifactId>
<version>7.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.30version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>${c3p0.version}version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>${taglibs.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.1version>
<scope>providedscope>
dependency>
dependencies>
<build>
<finalName>ssmday0628finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
其中,我们使用的ssm架构体现在java文件夹中,com.zhognruan可以看做是自定义的前缀文件夹,可以无需在意。
bean:实体类
dao:持久层,数据库增删改查
service:业务层
controller: 控制层,控制程序跳转
.jsp文件:相当于我们写的网页框架
UserMapper.xml:执行我们的sql语句
此处提供jsp文件及其他改动不多文件的下载,jsp文件可以不修改。
链接:https://pan.baidu.com/s/1yv2vkLs-JcJH7nR9_Dm_Kg
提取码:3brg
此处使用ModelAndView的对象来实现(以增操作为例),下面也会使用到其他的方法:
public ModelAndView toAddUser(){
ModelAndView mv=new ModelAndView();//新建一个web页面
mv.addObject("ui");//添加项目,如需添加参数,可在"ui"后用逗号分隔开,具体查看ModeAndView中的addObject方法
mv.setViewName("addUser");//连接addUser.jsp文件
return mv;//相当于显示界面(小白的理解)
}
对于页面的操作,归根结底,还是对数据库的操作,所以首先可以对dao层以及UserMapper.xml进行操作(以增操作为例),然后才是对业务层的操作,controller层相当于view层,开发由底层往表层会显得比较有层次,写完之后从表层往里层分析会更明白方法间的调用。
在IUserDao.java的接口里面添加save方法:
public boolean save(UserInfo userInfo);
xml文件中的语句比较简单,其中id的值对应dao层中预期对应的方法,parameterType对应方法中传递的参数,resultType对应方法的返回的参数,sql语句中的value值记得使用#{}包围起来。
<insert id="save" parameterType="UserInfo">
insert into userinfo(username,password) values(#{username},#{password})
insert>
首先在接口里声明方法save(),然后再在UserServiceImpl.java中重写实现方法。
//IUserService
public boolean save(UserInfo userInfo);
此处不需要自己编写代码,当接口类中声明了方法后,实现类会被标红,将光标指向实现类,Alt+Enter出现如图的提示,点击实现方法或者直接点击Enter,然后再点击OK,出现接口类中方法的实现,然后将false改成UserDao对象的方法出现此段 代码。
//UserServiceImpl
@Override
public boolean save(UserInfo userInfo) {
return userDao.save(userInfo);
}
首先在UserController中构造方法save,调用业务层中的save方法,将返回路径改为如下代码中的返回路径;
然后根据updateUser.jsp里的路径为在save方法上面添加@RequestMapping("/save.do")。
@RequestMapping("/save.do")//相当于查看文件时的路径
public String save(UserInfo userinfo){
userService.save(userinfo);
return "redirect:/user/findAll.do";
}
这便是添加操作的大致实现,需要修改的代码在下面会给出。
注意更改db.properties中的数据库信息
package com.zhongruan.controller;
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.ui.Model;
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;
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<UserInfo> userInfos=userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("ui",userInfos);
//控制跳转为什么不需要加后缀.jsp?
//见spring-mvc.xml即可
mv.setViewName("allUser");
return mv;
}
@RequestMapping("/toAddUser.do")
public ModelAndView toAddUser(){
ModelAndView mv=new ModelAndView();
mv.addObject("ui");
mv.setViewName("addUser");
return mv;
}
//此处也可仿照findAll方法中使用Modelandview对象实现
@RequestMapping("/toUpdate.do")
public String toUpdate(Model model,@RequestParam int id){
model.addAttribute("userInfo",userService.QueryUser(id));
return "updateUser";
}
@RequestMapping("/update.do")
public String update(UserInfo userInfo){
userService.update(userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("/save.do")
public String save(UserInfo userinfo){
userService.save(userinfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("/delete.do")
public String delete(int id){
userService.delete(id);
return "redirect:/user/findAll.do";
}
}
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public List<UserInfo> findAll();
public boolean save(UserInfo userInfo);
public boolean delete(int id);
public UserInfo QueryUser(int id);//查询ID为id的userinfo对象
public boolean update(UserInfo userInfo);
}
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List<UserInfo> findAll();
//public void toAddUser();
public boolean save(UserInfo userInfo);
public boolean delete(int id);
public UserInfo QueryUser(int id);
public boolean update(UserInfo userInfo);
}
package com.zhongruan.service.impl;
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 java.util.List;
@Service
public class UserSeviceImpl implements IUserService {
@Autowired
IUserDao userDao;
@Override
public List<UserInfo> findAll() {
return userDao.findAll();
}
@Override
public boolean save(UserInfo userInfo) {
return userDao.save(userInfo);
}
@Override
public boolean delete(int id) {
return userDao.delete(id);
}
@Override
public UserInfo QueryUser(int id) {
return userDao.QueryUser(id);
}
@Override
public boolean update(UserInfo userInfo) {
return userDao.update(userInfo);
}
}
<mapper namespace="com.zhongruan.dao.IUserDao" >
<select id="findAll" resultType="UserInfo">
select * from userinfo
select>
<insert id="save" parameterType="UserInfo">
insert into userinfo(username,password) values(#{username},#{password})
insert>
<delete id="delete" parameterType="UserInfo">
delete from userinfo where id=#{id}
delete>
<select id="QueryUser" resultType="UserInfo" parameterType="int">
select * from userinfo where id=#{id}
select>
<update id="update" parameterType="UserInfo">
update userinfo set username=#{username},password=#{password} where id=#{id}
update>
mapper>
注意bean包和dao包以及扫描的文件夹,仔细比较
<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">
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.zhongruan.bean">property>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhongruan.dao"/>
<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>tx:annotation-driven>
beans>
第2行中的db_ssm为数据库名
第3行中的为安装MySQL时的用户名
第4行中的为安装MySQL时的密码
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_ssm?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root