-
SSM(SSM 框架集)
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
页面

代码

1 package mySpringMVC;
2
3 import javaBean.Employee;
4 import javaBean.News;
5 import myConnection.DBConn;
6 import myConnection.Modify;
7 import org.springframework.stereotype.Controller;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.servlet.ModelAndView;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15 import java.util.Date;
16 import java.util.List;
17
18 @Controller
19 public class MyController {
20
21 //登录验证
22 @RequestMapping("/login")
23 public ModelAndView checkLogin(HttpServletRequest request){
24 HttpSession session = request.getSession();
25 Employee employee = new Employee();
26 employee.setName(request.getParameter("name"));
27 employee.setPassword(request.getParameter("password"));
28 Boolean isTrue = DBConn.check(employee);
29 session.setAttribute("isTrue",isTrue);
30 session.setAttribute("userName",request.getParameter("name"));
31 if (isTrue)
32 return setIndex();
33 else
34 return new ModelAndView("login");
35 }
36
37 //注销
38 @RequestMapping("/loginOut")
39 public ModelAndView loginOut(HttpServletRequest request){
40 HttpSession session = request.getSession();
41 session.setAttribute("isTrue",false);
42 return setIndex();
43 }
44
45 //点击单一新闻的请求响应
46 @RequestMapping("/info")
47 public ModelAndView dispatcherInfo(@RequestParam("type") String type,
48 @RequestParam("id") String id){
49 News news = null;
50 if ("imagenews".equals(type)){
51 news = DBConn.getNewsById(type,Integer.parseInt(id));
52 }
53 else if ("academiccommunication".equals(type)){
54 news = DBConn.getNewsById(type,Integer.parseInt(id));
55 }
56 else if ("couriernews".equals(type)){
57 news = DBConn.getNewsById(type,Integer.parseInt(id));
58 }
59 else if ("dynamicofparty".equals(type)){
60 news = DBConn.getNewsById(type,Integer.parseInt(id));
61 }
62 else if ("notifydynamic".equals(type)){
63 news = DBConn.getNewsById(type,Integer.parseInt(id));
64 }
65 else if ("projectlist".equals(type)){
66 news = DBConn.getNewsById(type,Integer.parseInt(id));
67 }
68 return getModel(news);
69 }
70
71 //设置网站的访问路径和全部新闻的显示,以及返回按钮访问新闻主页
72 @RequestMapping(value = {"/index","/back"})
73 public ModelAndView setIndex(){
74 ModelAndView modelAndView = new ModelAndView("index");
75 modelAndView.addObject("imagenewsList",DBConn.getNews("imagenews"));
76 modelAndView.addObject("academicList",DBConn.getNews("academiccommunication"));
77 modelAndView.addObject("couriernewsList",DBConn.getNews("couriernews"));
78 modelAndView.addObject("dynamicofpartyList",DBConn.getNews("dynamicofparty"));
79 modelAndView.addObject("notifydynamicList",DBConn.getNews("notifydynamic"));
80 modelAndView.addObject("projectList",DBConn.getNews("projectlist"));
81 return modelAndView;
82 }
83
84 //显示该类型的所有新闻
85 @RequestMapping("/newsPublish")
86 public ModelAndView getTypeNews(@RequestParam("type") String databaseName){
87 ModelAndView modelAndView = new ModelAndView("newsPublish");
88 List<News> list = DBConn.getAllNews(databaseName);
89 modelAndView.addObject("list",list);
90 modelAndView.addObject("name",databaseName);
91 return modelAndView;
92 }
93
94
95 //设置重定向的视图和数据
96 public ModelAndView getModel(News news){
97 ModelAndView modelAndView = new ModelAndView("info");
98 modelAndView.addObject("oneNews",news);
99 return modelAndView;
100 }
101
102 //新闻的修改
103 @RequestMapping("/upData")
104 public ModelAndView upData(@RequestParam("type") String databaseName, @RequestParam("id") int id){
105 ModelAndView modelAndView = new ModelAndView("addNews");
106 //数据库获取待修改的新闻信息
107 News news = DBConn.getNewsById(databaseName,id);
108 //将数据发送至新闻编辑页面进行填充
109 modelAndView.addObject("updata",news);
110 modelAndView.addObject("name",databaseName);
111 //设置修改操作的标识
112 Modify.modify = true;
113 //设置修改的新闻id
114 Modify.modifyId = id;
115 return modelAndView;
116 }
117
118 //添加新闻
119 @RequestMapping("AddNews")
120 public ModelAndView addNews(@RequestParam("title") String title, @RequestParam("type") String type,
121 @RequestParam("content") String content){
122
123 News news = new News();
124 news.setTitle(title);
125 news.setContent(content);
126 news.setTime(new Date());
127
128 if ("图片新闻".equals(type))
129 type = "imagenews";
130 else if ("学术交流".equals(type))
131 type = "academiccommunication";
132 else if ("新闻速递".equals(type))
133 type = "couriernews";
134 else if ("党建动态".equals(type))
135 type = "dynamicofparty";
136 else if ("通知公告".equals(type))
137 type = "notifydynamic";
138 else if ("专题列表".equals(type))
139 type = "projectlist";
140 else
141 type = "imagenews";
142
143 if (!Modify.modify){
144 //向数据库写入数据
145 DBConn.insertNews(news,type);
146 }
147 else{
148 news.setId(Modify.modifyId);
149 DBConn.upDataNewsById(news,type);
150 //重置Modify为默认状态
151 Modify.modify = false;
152 Modify.modifyId = -1;
153 }
154 return getTypeNews(type);
155 }
156
157 //添加新闻按钮事件
158 @RequestMapping("addNews")
159 public String add(){
160 return "addNews";
161 }
162
163 //删除新闻
164 @RequestMapping("/delete")
165 public ModelAndView deleteNews(@RequestParam("type") String type, @RequestParam("id") int id){
166 DBConn.deleteNews(type,id);
167 return getTypeNews(type);
168 }
169 }

1 MyController
2
3
4
5 package myConnection;
6
7 import javaBean.Database;
8 import javaBean.DatabaseName;
9 import javaBean.Employee;
10 import javaBean.News;
11 import org.apache.ibatis.io.Resources;
12 import org.apache.ibatis.session.SqlSession;
13 import org.apache.ibatis.session.SqlSessionFactory;
14 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.*;
19
20 public final class DBConn {
21 //连接数据库
22
23 public static SqlSession getSqlSession() {
24 String resource = "myXML/mybatis-config.xml";
25 InputStream inputStream = null;
26 SqlSession session = null;
27 try {
28 inputStream = Resources.getResourceAsStream(resource);
29 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
30 session = sqlSessionFactory.openSession();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 return session;
35 }
36
37 //校验用户信息
38 public static Boolean check(Employee emp){
39 SqlSession sqlSession = getSqlSession();
40 try {
41 //selectOne的第一个参数为Mapper.xml的mapper的namespace+id
42 //参数二为映射的需要传入的参数
43 Employee employee = sqlSession.selectOne(
44 "MyMapper.selectEmployee_name", emp.getName());
45 if (employee!=null && employee.getPassword().equals(emp.getPassword()))
46 return true;
47 else
48 return false;
49 } finally {
50 free(sqlSession);
51 }
52 }
53
54
55 //通过新闻表名获取该数据库的最新新闻
56 public static List<News> getNews(String databaseName){
57 int num = 1;
58 SqlSession sqlSession = getSqlSession();
59 //存在一个问题,直接传入String类型的参数时,纯mybatis无法获取参数,所以封装成一个Javabean
60 DatabaseName name = new DatabaseName();
61 name.setDatabaseName(databaseName);
62 List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
63 //只在首页界面显示最新的8条新闻
64 Collections.reverse(list);
65 Iterator<News> iterator = list.iterator();
66 List<News> temp = new ArrayList<>();
67 while (iterator.hasNext() && num<9){
68 temp.add(iterator.next());
69 num++;
70 }
71 free(sqlSession);
72 return temp;
73 }
74
75 //通过新闻表名获取该数据库的全部新闻
76 public static List<News> getAllNews(String databaseName){
77 SqlSession sqlSession = getSqlSession();
78 //存在一个问题,直接传入String类型的参数时,纯mybatis无法获取参数,所以封装成一个Javabean
79 DatabaseName name = new DatabaseName();
80 name.setDatabaseName(databaseName);
81 List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
82 free(sqlSession);
83 return list;
84 }
85
86
87
88 //通过id获取某一类别的某一条新闻
89 public static News getNewsById(String databaseName, int id){
90 //设置数据库名和新闻id
91 Database database = new Database();
92 database.setDatabaseName(databaseName);
93 database.setId(id);
94
95 SqlSession sqlSession = getSqlSession();
96 News news = sqlSession.selectOne("MyMapper.selectById",database);
97 free(sqlSession);
98 return news;
99 }
100
101
102 //释放Sqlsession
103 public static void free(SqlSession sqlSession){
104 sqlSession.close();
105 }
106
107 //向数据库添加新闻
108 public static void insertNews(News news, String type) {
109 SqlSession sqlSession = getSqlSession();
110 news.setDatabaseName(type);
111 System.out.println(news);
112 int result = sqlSession.insert("MyMapper.insertNews",news);
113 //提交事务
114 sqlSession.commit();
115 free(sqlSession);
116 }
117 //修改新闻信息
118 public static void upDataNewsById(News news, String type) {
119 // SqlSession sqlSession = getSqlSession();
120 // news.setDatabaseName(type);
121 // System.out.println(news);
122 // int res = sqlSession.update("MyMapper.updateNews",news);
123 // sqlSession.commit();
124 // free(sqlSession);
125 deleteNews(type,news.getId());
126 insertNews(news,type);
127 }
128
129 //删除新闻
130 public static void deleteNews(String databaseName,int id) {
131 SqlSession sqlSession = getSqlSession();
132 Database database = new Database();
133 database.setDatabaseName(databaseName);
134 database.setId(id);
135 int result = sqlSession.delete("MyMapper.deleteNews",database);
136 sqlSession.commit();
137 free(sqlSession);
138 }
139 }
来源:https://www.cnblogs.com/cappu-ccino/p/10890992.html
