《原神攻略》無名的寶藏三個在哪?無名的寶藏獲得方法介紹
SSM框架整合
SSM整合思路
Spring+SpringMVC+MyBatis,就是使用這三個框架的優勢完成一些專案的構建。三個框架分貝對應了三層架構中的一層。
- Spring:業務邏輯層
- SpringMVC:檢視層
- MyBatis:持久層
SSM整合,就需要把物件交給容器,讓容器去建立專案中要使用的物件,讓容器去建立專案中要使用的Java物件,目前有兩個容器。
- Spring容器,管理service物件和dao物件,是業務邏輯層物件的容器
- SpringMVC容器,這個容器是管理控制器物件,也就是檢視層物件。
兩個容器的建立
Spring容器的建立:在web.xml檔案中宣告監聽器 ContextLoaderListener,這個功能框架已經寫好了,就是建立Spring的容器物件WebApplicationContext,在建立WebApplicationContext物件時,讀取Spring配置檔案,遇到<bean>標籤或者註解,就可以建立service、dao物件,這些物件最終都放在Spring容器中。
SpringMVC容器的建立:在web.xml檔案中宣告中央排程器 DispatcherServlet,在這個servlet的init()方法中,建立了容器物件WebApplicationContext,在建立WebApplicationContext物件時,讀取SpringMVC的配置檔案,如果遇到相應的註解,則使用控制器物件,建立好的物件放到SpringMVC容器中。
SSM整合開發步驟
- 建立相應的資料庫和表(命令列方式或者通過視覺化工具都可)
- 在IDEA中使用maven、建立一個web專案
- 加入相關的依賴
- 在web.xml檔案中宣告容器物件
- 宣告spring的監聽器ContextLoaderListener:建立spring容器物件(service,dao)
- 宣告springmvc的中央排程器DispatcherServlet:建立springmvc容器的物件(controller)
- 宣告字符集過濾器CharacterEncodingFilter,解決post請求亂碼的問題。
具體步驟
建立相應的表和資料庫
建立一個add資料庫,和一個student表,只有三個資料(id,name,age)
專案整體結構
加入相關依賴
<!-- servlet依賴 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- jsp依賴 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> <scope>provided</scope> </dependency> <!-- jackson依賴 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <!-- spring依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- spring事務依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- springmvc依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- mybatis依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <!-- mybatis-spring整合依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!-- mysql驅動依賴 --> <!-- druid連線池依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> </dependencies>
使用的時候要和自己的版本一樣呀,尤其是mysql的版本
在web.xml檔案中,宣告容器物件
<!-- 宣告字符集過濾器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 宣告springmvc的中央排程器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 宣告spring監聽器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
注意:字符集過濾器放到宣告的第一個
建立包
dao,controller,service,entity(model,pojo)
編寫mybatis,spring,springmvc的配置檔案
- mybatis配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 設定日誌 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<mappers>
<!-- 載入dao包下的所有mapper檔案 -->
<package name="com.wangyingao.dao"/>
</mappers>
</configuration>
這裡有一個易錯點,載入了相應的mapper檔案,但是並沒有把他放進編譯後的檔案中去,後續進行MyBatis的資料庫操作時會找不到XxxxDao.xml檔案的錯誤,給出的解決建議是將下面的程式碼複製到pom.xml檔案中,是為了將src/main/java目錄下的xml、properties檔案都能被掃描到。
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目錄-->
<includes><!--包括目錄下的.properties,.xml檔案都會掃描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- spring配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring配置檔案:宣告service、dao、工具類、事務配置 -->
<!-- <!– 載入外部屬性配置檔案 –>-->
<!-- 宣告元件掃描器 -->
<context:component-scan base-package="com.wangyingao.service" />
<!-- 建立資料來源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="你應該會" />
<property name="username" value="你應該會" />
<property name="password" value="你應該會" />
</bean>
<!-- 建立SqlSessionFactory物件 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/conf/mybatis.xml" />
</bean>
<!-- 建立SqlSession物件,通過反射機制載入dao介面對應的mapper檔案 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory" />
<property name="basePackage" value="com.wangyingao.dao" />
</bean>
<!-- 事務配置 -->
</beans>
- springmvc的配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- springmvc配置檔案:宣告controller、檢視解析器等web開發中的物件 -->
<!-- 宣告元件掃描器 -->
<context:component-scan base-package="com.wangyingao.controller" />
<!-- 宣告檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 宣告springmvc註解驅動 -->
<mvc:annotation-driven />
</beans>
編寫java程式碼
- 實體類
public class Student {
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
- dao介面和mapper檔案
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudent();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wangyingao.dao.StudentDao">
<!-- 使用insert、update、delete、select標籤編寫sql語句 -->
<insert id="insertStudent">
insert into student(name,age) values (#{name},#{age})
</insert>
<select id="selectStudent" resultType="com.wangyingao.entity.Student">
select id,name,age from student order by id
</select>
</mapper>
- service介面和實現類
public interface StudentService {
int addStudent(Student student);
List<Student> queryStudent();
}
@Service
public class StudentServiceImpl implements StudentService {
/**
* studentDao是引用型別,其物件是在spring配置檔案中建立
* 引用型別自動注入,這裡使用註解 @Autowired 或者 @Resource
*/
@Autowired
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
int rows = studentDao.insertStudent(student);
return rows;
}
@Override
public List<Student> queryStudent() {
List<Student> list = studentDao.selectStudent();
return list;
}
}
建立一個控制器類(接受並處理請求)
@Controller
@RequestMapping(value = "/student")
public class StudentController {
/**
* 宣告service物件,呼叫其中的方法
* 引用型別自動注入,這裡使用註解 @Autowired 或者 @Resource
*/
@Autowired
private StudentService studentService;
//新增學生
@RequestMapping(value = "/addStudent.do")
public ModelAndView addStudent(Student student) {
ModelAndView mv=new ModelAndView();
//呼叫service,處理業務邏輯方法,把處理結果返回給使用者
int rows=studentService.addStudent(student);
String msg="";
if (rows>0) {
msg="註冊成功!!!";
mv.addObject("msg",student.getName() + "," + student.getAge());
mv.setViewName("success");
}else {
msg="註冊失敗!!!";
mv.addObject("msg",msg);
mv.setViewName("fail");
}
return mv;
}
@RequestMapping(value = "/queryStudent.do")
@ResponseBody
public List<Student> queryStudent() {
return studentService.queryStudent();
}
}
建立檢視檔案(直接貼程式碼)
- 首頁(index.jsp)
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%
String basePath=request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>SSM</title>
</head>
<body>
<div align="center">
<p>SSM整合開發的例子</p>
<table>
<tr>
<td><a href="addStudent.jsp">註冊學生</a></td>
<td><br/></td>
<td><a href="queryStudent.jsp">查詢學生</a></td>
</tr>
</table>
</div>
</body>
</html>
- 註冊學生頁面(addStudent.jsp)
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
<title>新增學生</title>
</head>
<body>
<div align="center">
<p>註冊學生</p>
<form action="student/addStudent.do" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年齡:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>提交:</td>
<td><input type="submit" value="註冊"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
- 查詢學生頁面(queryStudent.jsp)
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%
String basePath=request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>查詢學生</title>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript">
$(function () {
$("#myBtn").on("click",function () {
$.ajax({
url: "student/queryStudent.do",
dataType: "json",
success : function (resp) {
$("#stuinfo").empty();
$.each(resp,function (i,n) {
$("#stuinfo").append("<tr><td>" + n.id + "</td>" +
"<td>" + n.name + "</td>" +
"<td>" + n.age + "</td></tr>");
})
}
})
})
})
</script>
</head>
<body>
<div align="center">
<p>查詢學生 <button id="myBtn">獲取學生資訊</button></p>
<table>
<thead>
<tr>
<td>id</td>
<td>姓名</td>
<td>年齡</td>
</tr>
</thead>
<tbody id="stuinfo">
</tbody>
</table>
</div>
</body>
</html>
- 註冊成功和失敗的頁面(succes.jsp fail.jsp),兩者程式碼一樣
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
<title>$</title>
</head>
<body>
<h3>結果:${msg}</h3>
</body>
</html>
執行即可
注:
本篇文章完全參考:
SSM整合——簡單的小專案實戰_張起靈-小哥的部落格-CSDN部落格_ssm專案
部分參考: