这个小demo是在学习SSM框架是用来熟悉框架在网上找到的一个小例子,感觉页面还不错,比较美观,适合学习SSM的新手。
完整的项目下载:https://download.csdn.net/download/ydw_ydw/12186575
(安装Maven以及如何新建Maven项目请自行搜索学习)
1、新建Maven项目要选择war包。
2、通常新建的maven项目会报错,这是因为没有web.xml,解决此报错很简单,直接在项目上右键【Java EE Tools】-【generate deployment xxx】,然后就不报错了(如果新建Maven项目中如下选择xxxx-webapp就不会出现这样的错误)。
3、在代码开始之前需要去网上下载如下几个文件(或直接在下载的项目内复制出来)
bootstrap.min.css 和 bootstrap.min.js 和 jquery-3.3.1.min.js
用maven的好处之一就是不用到处找jar包,想要什么jar包直接在pom.xml里面添加依赖就可以了。想要依赖可以去中央仓库找
我们可以把常用的依赖收集起来,以后可以直接复制pom.xml(前提里面的jar包个各个版本需要对应,不对应可能会出现错误,大家可以在网上搜索一个所有版本对应的模板)。
1、首先是配置文件
pom.xml
4.0.0
com.lijiang
ssm_spring_mvc_maven
0.0.1-SNAPSHOT
war
ssm_spring_mvc_maven
用maven做的ssm_spring_mvc项目
5.0.8.RELEASE
3.4.6
1.3.2
5.1.46
12.1.0.2.0
1.0.16
1.0
1.6.4.RELEASE
1.1.1
1.2
1.2.16
1.6.1
1.6.4
1.3.1
2.9.6
4.9
8.0.53
8.0.53
0.9.5.2
3.8.0
2.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybais.version}
org.mybatis
mybatis-spring
${mybatis-spring.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.mchange
c3p0
${c3p0.version}
org.springframework
spring-core
org.springframework
spring-web
org.springframework
spring-tx
org.springframework
spring-jdbc
org.springframework
spring-webmvc
org.springframework
spring-aop
org.springframework
spring-context-support
org.springframework
spring-test
org.mybatis
mybatis
org.mybatis
mybatis-spring
mysql
mysql-connector-java
${mysql-connector.version}
cn.easyproject
ojdbc7
${ojdbc7.version}
com.alibaba
druid
${druid.version}
aopalliance
aopalliance
${aopalliance.version}
org.aspectj
com.springsource.org.aspectj.weaver
${aspectj.weaver.version}
org.apache.commons
com.springsource.org.apache.commons.logging
${commons.logging.version}
javax.servlet
jstl
${jstl.version}
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j-api.version}
org.slf4j
slf4j-nop
${slf4j-nop.version}
commons-fileupload
commons-fileupload
${fileupload.version}
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-annotations
com.fasterxml.jackson.core
jackson-databind
junit
junit
${junit.version}
test
org.apache.tomcat
tomcat-servlet-api
${servlet-api.version}
provided
org.apache.tomcat
tomcat-jsp-api
${jsp-api.version}
provided
com.mchange
c3p0
${c3p0.version}
org.apache.maven.plugins
maven-compiler-plugin
${maven-compiler-plugin.version}
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
${tomcat7-maven-plugin.version}
UTF-8
http://localhost:8080/manager/text
lijiang
111
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
springmvc.xml
db.properties(连接信息可根据自己的不同自己修改,或者参考之前自己做过的连接数据库的项目去修改)
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
applicationContext_mapper.xml
applicationContext_service.xml
applicationContext_transaction.xml
2、代码的编写(实现的顺序是:持久层——dao层——service层——controller层——jsp层)
ItemInfo.java
public class ItemInfo {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public ItemInfo(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
}
ItemMapper.java
public interface ItemMapper {
List selectAll(ItemInfo itemInfo);
void deleteById(Integer id);
void saveItem(ItemInfo itemInfo);
void updateItem(ItemInfo itemInfo);
ItemInfo selectItemInfoById(Integer id);
}
ItemMapper.xml(代码第五行要引入刚才ItemMapper.java的位置)
delete from user where id=#{id}
insert into user values(#{id},#{username}, #{password});
update user
username = #{username},
password = #{password}
where id = #{id}
ItemService.java
public interface ItemService {
List selectAll(ItemInfo itemInfo);
void deleteById(Integer id);
void saveItem(ItemInfo itemInfo);
void updateItem(ItemInfo itemInfo);
ItemInfo selectItemInfoById(Integer id);
}
ItemServicelmpl.java
@Service
public class ItemServicelmpl implements ItemService {
@Autowired
private ItemMapper itemMapper;
public List selectAll(ItemInfo itemInfo) {
return itemMapper.selectAll(itemInfo);
}
public void deleteById(Integer id) {
itemMapper.deleteById(id);
}
public void saveItem(ItemInfo itemInfo) {
itemMapper.saveItem(itemInfo);
}
public void updateItem(ItemInfo itemInfo) {
itemMapper.updateItem(itemInfo);
}
public ItemInfo selectItemInfoById(Integer id) {
return itemMapper.selectItemInfoById(id);
}
}
ItemController.java
@Controller
@RequestMapping("/admin/items")
public class ItemController {
@Autowired
private ItemService itemService;
// 显示所有列表
@RequestMapping("allList.do")
public String selectAll(Model model,ItemInfo itemInfo){
List itemList=itemService.selectAll(itemInfo);
model.addAttribute("itemList", itemList);
return "item_list";
}
// 删除
@RequestMapping("delete.do")
public String delete(Integer id){
itemService.deleteById(id);
return "forward:allList.do";
}
// 添加
@RequestMapping("save.do")
public String save(ItemInfo itemInfo){
itemService.saveItem(itemInfo);
return "forward:allList.do";
}
// 修改
@RequestMapping("update.do")
public String update(ItemInfo itemInfo){
itemService.updateItem(itemInfo);
return "forward:allList.do";
}
// 编辑 回显数据
@RequestMapping("edit.do")
@ResponseBody
public ItemInfo edit(Integer id) {
return itemService.selectItemInfoById(id);
}
}
item_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Insert title here
id
username
password
${item.id}
${item.username}
${item.password}