想要提高编程水平吗,手把手教你SSM的框架整(CRUD)

之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些。以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下。理解其思想,这对于我们提高编程水平很有帮助。不过,如果用都不会,谈思想就变成纸上谈兵了!!

1,基本概念

1.1 Spring

Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

1

1.2 SpringMVC

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制

1.3 Mybatis

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

2.开发环境搭建

1.windows10+mysql5.5+Maven+bootstrap+jQuery+jdk1.8

2.我的包结构

3.SSM项目整合

引入相关依赖(pom.xml)

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.youcai

SSM_CRUD

war

0.0.1-SNAPSHOT

SSM_CRUD Maven Webapp

http://maven.apache.org

junit

junit

4.12

test

org.springframework

spring-test

4.3.21.RELEASE

test

org.mybatis

mybatis

3.4.5

mysql

mysql-connector-java

5.1.43

org.springframework

spring-webmvc

4.3.10.RELEASE

org.springframework

spring-jdbc

4.3.10.RELEASE

org.mybatis

mybatis-spring

1.3.1

com.mchange

c3p0

0.9.5.1

javax.servlet

javax.servlet-api

4.0.1

provided

jstl

jstl

1.2

com.github.pagehelper

pagehelper

4.2.1

org.springframework

spring-core

4.3.10.RELEASE

SSM_CRUD

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394

2.实体类代码块(User)

packagecom.youcai.entity;

publicclassUser{

privateintid;

privateString username;

privateString password;

privateString phone;

publicintgetId(){

returnid;

}publicvoidsetId(intid){

this.id = id;

}publicStringgetUsername(){

returnusername;

}publicvoidsetUsername(String username){

this.username = username;

}publicStringgetPassword(){

returnpassword;

}publicvoidsetPassword(String password){

this.password = password;

}publicStringgetPhone(){

returnphone;

}publicvoidsetPhone(String phone){

this.phone = phone;

}publicUser(intid, String username, String password, String phone){

super();

this.id = id;

this.username = username;

this.password = password;

this.phone = phone;

}publicUser(String username, String password, String phone){

super();

this.username = username;

this.password = password;

this.phone = phone;

}publicUser(){

super();

// TODO Auto-generated constructor stub

}

@Override

public String toString() {

return "User [id=" + id + ", username=" + username + ", password=" + password + ", phone=" + phone + "]";

}

}

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455

3.User.xml文件

PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert intouser(username,password,phone) values(#{username},#{password},#{phone})

select * from user

select * from user where id=#{id}

select * from user  limit pageNum=#{pageNum}

delete from user where id=#{id};

update user set username=#{username},password=#{password},phone=#{phone} where id= #{id};

12345678910111213141516171819202122232425262728293031

4.dao层接口

packagecom.youcai.dao;

importjava.util.List;

importcom.youcai.entity.User;

publicinterfaceUserDao{

publicvoidaddUser(User user);

publicintdeleteUser(intid);

publicListgetUser();

publicintupdateUser(User user);

publicUsergetById(intid);

ListgetList(intpage,intsize);

}123456789101112131415161718192021

5.service层(这里的service层只写了一个,用来返回分页数据的,分页使用的是pageHelper)

packagecom.youcai.service;

importjava.util.List;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Service;

importcom.youcai.dao.UserDao;

importcom.youcai.entity.User;

@Service

publicclassUserService{

@Autowired

UserDao userDao;publicList getAllUser() {

returnuserDao.getUser();

}}123456789101112131415161718192021

6.控制层(我基本的逻辑都是写在Controller里面 练手的小demo就没有那么规范啦,这边的删除功能目前还没有写)

/**

* 分页

*@return

*/

@Autowired

UserService userService;@RequestMapping("/getUser")

publicString getUser(@RequestParam(value="pn",defaultValue="1")int pn,Model model){

PageHelper.startPage(pn,5);

List users= userService.getAllUser();System.out.println(users);

PageInfo page = new PageInfo(users,5);

model.addAttribute("pageInfo", page);

return"list";

}/**

* 添加

*/

@Autowired

UserDao userDao;@RequestMapping("/addUser")

publicModelAndView addUser(User user){

userDao.addUser(user);System.out.println("添加成功,添加的对象是"+user.toString());

List list=userDao.getUser();ModelAndView andView=new ModelAndView("show");

andView.addObject("list", list);

returnandView;

}/**

* 删除

*/

@RequestMapping("/delete")

publicString deleteUser(@RequestParam("id")int id,Model model){

userDao.deleteUser(id);List users= userService.getAllUser();System.out.println(users);

PageInfo page = new PageInfo(users,5);

model.addAttribute("pageInfo", page);

System.out.println("删除成功,删除的对象的id是"+id);

return"show";

}/**

* 修改之前

*@paramid

*@return

*/

@RequestMapping("/upodate")

publicModelAndView getById(int id){

User users = userDao.getById(id);ModelAndView view=new ModelAndView("update");

System.out.println("得到的id为"+id);

System.out.println("修改对象是:"+users.toString());

view.addObject("users", users);

returnview;

}/**

* 修改之后

*@paramuser

*@parammap

*@return

*/

@RequestMapping("/updateUser")

publicModelAndView updateUser(User user,Model model){

System.out.println("修改后的对象是:"+user.toString());

int updateUser = userDao.updateUser(user);System.out.println(updateUser);

System.out.println("修改成功");

List users= userService.getAllUser();PageInfo page = new PageInfo(users,5);

model.addAttribute("pageInfo", page);

ModelAndView view=new ModelAndView("show");

returnview;

}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182

7.配置文件

7.1 application.xml()

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

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-4.3.xsd">

dialect=mysqlreasonable=true

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

7.2 db.properties(用来链接数据库的,配置用户名,密码等等)

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test

jdbc.username=rootjdbc.password=rootinitPoolSize=5

maxPoolSize=10

123456

7.3springmvc.xml(配置扫描器,和视图解析器)

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"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

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-4.3.xsd">

1234567891011121314151617181920

7.4 web.xml(配置前端过滤器)

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

Archetype Created Web Application

contextConfigLocation

classpath:applicationContext.xml

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encoding

/*

org.springframework.web.context.ContextLoaderListener

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springMVC.xml

1

springDispatcherServlet

/

有什么问题欢迎大家在评论区留言讨论,如果想要一起学习Python,可以关注小编,之后会有更多干货知识分享,大家一起学习一起进步~

你可能感兴趣的:(想要提高编程水平吗,手把手教你SSM的框架整(CRUD))