标签:set lse 查询 动态 actor oca web else local
一、项目创建:
1、创建一个Javaweb项目并且配置好tomcat等
(之前做过 此步省略)
2、在setting中引入MAVEN
注意:
MAVEN 的版本不能早于编辑器的版本否则会报错。
3、创建项目结构
二、代码编写:
1、在pom.xml中引入需要的包的信息
2、创建spring的配置文件applicationContext.xml,并整合SpringMVC和Mybatis
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 9 <!-- 加载数据库连接信息配置文件 --> 10 <context:property-placeholder location="classpath:db.properties" /> 11 12 <!--druid连接池 --> 13 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 14 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> 15 <property name="url" value="jdbc:mysql://localhost:3306/javaee?serverTimezone=GMT;characterEncoding=utf-8"/> 16 <property name="username" value="${druid.username}"/> 17 <property name="password" value="${druid.password}"/> 18 </bean> 19 <!-- 配置Mybatis工厂 --> 20 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 21 <property name="dataSource" ref="dataSource" /> 22 <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 23 </bean> 24 25 <!-- Mapper动态代理开发 扫包 给定包下的接口文件名和映射文件名必须相同 创建接口的实现类--> 26 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 27 <property name="basePackage" value="com.xiao.mapper" /> 28 </bean> 29 30 <!-- 开启事物 --> 31 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 32 <property name="dataSource" ref="dataSource" /> 33 </bean> 34 <!-- 事物注解驱动--> 35 <tx:annotation-driven /> 36 37 </beans>
3、创建SpringMVC配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 8 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> 9 10 <!-- 配置扫描注解 @Controller @Service --> 11 <context:component-scan base-package="com" /> 12 13 <!-- SpringMVC使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter --> 14 <mvc:annotation-driven /> 15 16 <!-- 配置静态资源映射 --> 17 18 <mvc:resources location="/js/" mapping="/js/**"/> 19 <mvc:resources location="/css/" mapping="/css/**"/> 20 <mvc:resources location="/images/" mapping="/images/**"/> 21 <mvc:resources location="/img_source/" mapping="/img_source/**"/> 22 <mvc:resources location="/ueditor/" mapping="/ueditor/**"/> 23 <mvc:resources location="/utilLib/" mapping="/utilLib/**"/> 24 25 <!-- 配置视图解析器--> 26 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 27 <!-- 配置逻辑视图的前缀 --> 28 <property name="prefix" value="/WEB-INF/jsp/" /> 29 <!-- 配置逻辑视图的后缀 --> 30 <property name="suffix" value=".jsp" /> 31 32 </bean> 33 34 </beans>
4、编写实体类
1 package com.entity; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 @Data 8 @AllArgsConstructor 9 @NoArgsConstructor 10 public class User { 11 String userID; 12 String userPWD; 13 14 public String getUsername() { 15 return userID; 16 } 17 18 public void setUsername(String username) { this.userID = username; } 19 20 public String getPassword() { 21 return userPWD; 22 } 23 24 public void setPassword(String password) { 25 this.userPWD = password; 26 } 27 }
1 package com.entity; 2 import lombok.AllArgsConstructor; 3 import lombok.Data; 4 import lombok.NoArgsConstructor; 5 6 @Data 7 @AllArgsConstructor 8 @NoArgsConstructor 9 public class News { 10 private int newsId; 11 private String newsContent; 12 private String newsAuthor; 13 private String newsTitle; 14 15 public int getNewsId() { 16 return newsId; 17 } 18 19 public void setNewsId(int newsId) { 20 this.newsId = newsId; 21 } 22 23 public String getNewsContent() { 24 return newsContent; 25 } 26 27 public void setNewsContent(String newsContent) { 28 this.newsContent = newsContent; 29 } 30 31 public String getNewsAuthor() { 32 return newsAuthor; 33 } 34 35 public void setNewsAuthor(String newsAuthor) { 36 this.newsAuthor = newsAuthor; 37 } 38 39 public String getNewsTitle() { 40 return newsTitle; 41 } 42 43 public void setNewsTitle(String newsTitle) { 44 this.newsTitle = newsTitle; 45 } 46 47 }
5、service层
1 package com.service; 2 3 import com.entity.News; 4 5 import java.sql.*; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 public class NewsService { 10 //添加新闻 11 public boolean AddNews(News news) throws ClassNotFoundException, SQLException { 12 Statement statement; 13 Connection conn ; 14 Class.forName("com.mysql.cj.jdbc.Driver"); 15 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8","root","1234567890"); 16 String sql="insert into new_schema.news (newsID,newsTitle,newsContent,newsAuthor) " + 17 "values (‘"+news.getNewsId()+"‘,‘"+news.getNewsTitle()+"‘,‘"+news.getNewsContent()+"‘,‘"+news.getNewsAuthor()+"‘)"; 18 statement=conn.createStatement(); 19 int result =statement.executeUpdate(sql); 20 if (result>0)return true; 21 else return false; 22 } 23 24 //删除新闻 25 public boolean DeleteNews(int newsId) throws ClassNotFoundException, SQLException { 26 Statement statement = null; 27 Connection conn ; 28 Class.forName("com.mysql.cj.jdbc.Driver"); 29 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8","root","1234567890"); 30 String sql= "delete from new_schema.news where newsID="+newsId; 31 statement=conn.createStatement(); 32 int result = statement.executeUpdate(sql); 33 if(result>0)return true; 34 else return false; 35 36 } 37 38 //更新新闻 39 public boolean UpdateNews(News news) throws ClassNotFoundException, SQLException { 40 Statement statement = null; 41 Connection conn ; 42 ResultSet rs; 43 Class.forName("com.mysql.cj.jdbc.Driver"); 44 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8","root","1234567890"); 45 String sql="update new_schema.news set newsTitle =‘"+news.getNewsTitle()+"‘, newsContent =‘"+news.getNewsContent()+"‘, newsAuthor =‘"+news.getNewsAuthor()+"‘ where newsID="+news.getNewsId(); 46 statement = conn.createStatement(); 47 int result = statement.executeUpdate(sql); 48 if(result>0)return true; 49 else return false; 50 } 51 52 //查询新闻 53 public News QueryIndividualNews(int newsID)throws ClassNotFoundException, SQLException { 54 Statement statement = null; 55 Connection conn; 56 ResultSet rs; 57 Class.forName("com.mysql.cj.jdbc.Driver"); 58 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news?serverTimezone=GMT&characterEncoding=utf-8", "root", "1234567890"); 59 String sql = "Select * from new_schema.news where newsID=" + newsID; 60 statement = conn.createStatement(); 61 rs = statement.executeQuery(sql); 62 63 News n = new News(); 64 if (rs.next()) { 65 n.setNewsId(rs.getInt("newsID")); 66 n.setNewsTitle(rs.getString("newsTitle")); 67 n.setNewsAuthor(rs.getString("newsAuthor")); 68 n.setNewsContent(rs.getString("newsContent")); 69 } 70 return n; 71 } 72 73 //查询数据库内所有新闻 74 public List<News> QueryNews() throws ClassNotFoundException, SQLException { 75 Statement statement = null; 76 Connection conn ; 77 ResultSet rs; 78 Class.forName("com.mysql.cj.jdbc.Driver"); 79 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news?serverTimezone=GMT&characterEncoding=utf-8","root","Guozhaojie610"); 80 String sql = "Select * from new_schema.news"; 81 82 statement=conn.createStatement(); 83 rs = statement.executeQuery(sql); 84 85 List<News> NewsList = new ArrayList<News>(); 86 87 while (rs.next()){ 88 News n = new News(); 89 n.setNewsId(rs.getInt("newsID")); 90 n.setNewsTitle(rs.getString("newsTitle")); 91 n.setNewsAuthor(rs.getString("newsAuthor")); 92 n.setNewsContent(rs.getString("newsContent")); 93 NewsList.add(n); 94 } 95 return NewsList; 96 } 97 98 99 }
6、controller层
1 package com.controller; 2 3 import com.entity.News; 4 import com.entity.User; 5 import com.service.NewsService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 11 import javax.servlet.ServletException; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import java.io.IOException; 15 import java.io.UnsupportedEncodingException; 16 import java.sql.SQLException; 17 import java.util.Arrays; 18 import java.util.List; 19 20 public class NewsController { 21 22 @Autowired 23 private NewsService newsService; 24 25 //显示所有新闻 26 @RequestMapping("/ShowNews") 27 public void ShowNews(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { 28 request.setCharacterEncoding("utf-8"); 29 response.setCharacterEncoding("utf-8"); 30 try { 31 List<News> newsList = newsService.QueryNews(); 32 for (News s:newsList 33 ) { 34 System.out.println(s.getNewsId()); 35 } 36 37 request.setAttribute("News", newsList); 38 request.getRequestDispatcher("ShowNews.jsp").forward(request, response); 39 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 } 44 45 @RequestMapping("/AddNews") 46 public void addNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 47 request.setCharacterEncoding("utf-8"); 48 response.setCharacterEncoding("utf-8"); 49 response.setHeader("Context-Type","text/html;charset=utf-8"); 50 News news =new News(); 51 news.setNewsId(Integer.valueOf(request.getParameter("newsid"))); 52 news.setNewsAuthor(request.getParameter("author")); 53 news.setNewsContent(request.getParameter("Content")); 54 news.setNewsTitle(request.getParameter("title")); 55 NewsService newsService=new NewsService(); 56 try { 57 newsService.AddNews(news); 58 request.getRequestDispatcher("ShowNews").forward(request,response); 59 } catch (ClassNotFoundException e) { 60 e.printStackTrace(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 } 65 66 @RequestMapping("/UpdateNews") 67 public void editNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 68 response.setContentType("text/html; charset=UTF-8"); 69 request.setCharacterEncoding("UTF-8"); 70 response.setHeader("Context-Type", "text/html;charset=utf-8"); 71 News news = new News(); 72 news.setNewsId(Integer.valueOf(request.getParameter("newsid"))); 73 news.setNewsAuthor(request.getParameter("author")); 74 news.setNewsContent(request.getParameter("Content")); 75 news.setNewsTitle(request.getParameter("title")); 76 NewsService newsService = new NewsService(); 77 try { 78 newsService.UpdateNews(news); 79 request.getRequestDispatcher("ShowNews").forward(request, response); 80 } catch (ClassNotFoundException e) { 81 e.printStackTrace(); 82 } catch (SQLException e) { 83 e.printStackTrace(); 84 } 85 } 86 87 88 @RequestMapping("/deleteNew") 89 public void deleteNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 90 91 int newsID=Integer.valueOf(request.getParameter("newsid")); 92 NewsService newsService=new NewsService(); 93 try { 94 newsService.DeleteNews(newsID); 95 } catch (ClassNotFoundException e) { 96 e.printStackTrace(); 97 } catch (SQLException e) { 98 e.printStackTrace(); 99 } 100 request.getRequestDispatcher("com.controller.ShowNewsServlet").forward(request,response); 101 } 102 103 @RequestMapping("/viewNew") 104 public void viewNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 105 int newsID=Integer.valueOf(request.getParameter("newsid")); 106 NewsService newsService=new NewsService(); 107 try { 108 News news=newsService.QueryIndividualNews(newsID); 109 request.setAttribute("news",news); 110 request.getRequestDispatcher("NewsDetail.jsp").forward(request,response); 111 }catch (ClassNotFoundException e) { 112 e.printStackTrace(); 113 } catch (SQLException e) { 114 e.printStackTrace(); 115 } 116 } 117 118 }
package com.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; @Controller public class UserController { @RequestMapping("/Login") public void login(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); response.setHeader("Context-Type", "text/html;charset=utf-8"); PrintWriter output = response.getWriter(); String userID = request.getParameter("username"); String userPWD = request.getParameter("pwd"); ResultSet rs; Statement statement = null; Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8", "root", "1234567890"); String sql = "select * from userinfo where id=‘" + userID + "‘ and password=‘" + userPWD + "‘"; statement = conn.createStatement(); rs = statement.executeQuery(sql); if (rs.next()) { request.setAttribute("username", userID); response.sendRedirect("index.html"); } else { output.println("账号或密码错误"); response.sendRedirect("index.jsp"); } } catch (Exception e) { e.printStackTrace(); } } }
7、web.xml的修改
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 6 version="2.5"> 7 8 <display-name>SSM</display-name> 9 10 <welcome-file-list> 11 <welcome-file>index.jsp</welcome-file> 12 </welcome-file-list> 13 14 <!-- spring监听器 --> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 19 <!-- 指定spring核心配置文件 --> 20 <context-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:applicationContext.xml</param-value> 23 </context-param> 24 25 <!-- 处理POST提交乱码问题 --> 26 <filter> 27 <filter-name>encoding</filter-name> 28 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 29 <init-param> 30 <param-name>encoding</param-name> 31 <param-value>UTF-8</param-value> 32 </init-param> 33 </filter> 34 <filter-mapping> 35 <filter-name>encoding</filter-name> 36 <url-pattern>/*</url-pattern> 37 </filter-mapping> 38 39 <!-- 配置前端控制器 --> 40 <servlet> 41 <servlet-name>springmvc</servlet-name> 42 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 43 <!-- 指定配置文件位置和名称 如果不设置,默认找/WEB-INF/<servlet-name>-servlet.xml --> 44 <init-param> 45 <param-name>contextConfigLocation</param-name> 46 <param-value>classpath:springmvc.xml</param-value> 47 </init-param> 48 <load-on-startup>1</load-on-startup> 49 </servlet> 50 <servlet-mapping> 51 <servlet-name>springmvc</servlet-name> 52 <url-pattern>/</url-pattern> 53 </servlet-mapping> 54 55 <filter> 56 <filter-name>AFilter</filter-name> 57 <filter-class>com.Filter.AFilter</filter-class> 58 </filter> 59 <filter-mapping> 60 <filter-name>AFilter</filter-name> 61 <url-pattern>/AFilter</url-pattern> 62 </filter-mapping> 63 64 </web-app>
三、效果展示:
1、新闻首页
2、登录界面
3、后台添加新闻
4、新闻管理
标签:set lse 查询 动态 actor oca web else local
原文地址:https://www.cnblogs.com/lbs727299681/p/13196721.html