ヒント: 記事を作成した後、目次を自動的に生成できます。生成方法は、右側のヘルプドキュメントを参照してください。
序文
コース設計プロジェクトを行う前にSSMの使い方を学ぶためのハンズオンプロジェクトであり、フレームワークの構築、リストの追加、削除、変更、ページング、ページング機能の実現などが含まれます。
1. フレームワークの構築
私の SSM フレームワーク構築はステーション b で学びました. フレームワーク構築は自分で書く必要はありません. 他の場所からフレームワークをコピーして学習するだけで十分です. フレームワーク内の各位置の役割を知っていれば十分です.
ここにフレームワーク研究のビデオが添付されています。UP の講義は非常に明確で、対応するドキュメントもあります。
SSM フレームワークには、Dao レイヤー、サービス レイヤー、およびコントローラー レイヤーの 3 つのセクションがあります。
Dao 層は最下層であり、データベース内のデータの呼び出しと変更を担当します。サービス層は中間層で、Dao 層とコントローラー層の間のデータ送信と接続を担当します。コントローラー層は最上層です。 、Web ページからの関連データを処理し、Web ページにデータを返す責任があります。
たとえば、レストラン。
顧客(ウェブページ)が食事をしに来ると、まずウェイター(コントローラー層)に料理を注文(データリクエスト)し、ウェイターは顧客が注文した料理(データリクエスト)をバックキッチンウィンドウ(サービス層)に書き込み、ウィンドウの要件に応じて、バックキッチン(Dao レイヤー)と食材(データベースデータ)を処理してウィンドウに置くと、ウェイターが料理を顧客に運びます。
SSM の一般的な役割をこのように理解すると理解しやすいでしょうか?
次に、具体的なプロジェクトである学生情報のリストについて説明しますが、Spring、SpringMVC、MyBatis の 3 つの主要なセクションを学習した後に統合を学習することをお勧めします。
最終的に、下の画像に示すように、フレーム付きのファイルが作成されました。
jsp ページ ファイルを次の図に示します。後続の設定ファイルは、jsp ファイルのプレフィックスとサフィックスを設定します。
web.xml の役割は、Web ページの URL リクエストをインターセプトして配布するために使用される DispatcherServlet を構成することです。
次のフィルタは、文字化けエンコードの問題を解決するために使用されます。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>springLearn</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--注意这里不能配置成/*-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
このフレームワークは、applicationContext.xml の内容を 3 つの部分に分割して記述されており、このファイルは 3 つのサブファイルを統合するものです。
applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
Mybatis-config.xml ファイルは、エイリアスの設定と dao 層インターフェイス ファイルのスキャンに使用されます。
<?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>
<!-- 配置别名 -->
<typeAliases>
<package name="com.pojo"/>
</typeAliases>
<!-- 配置扫描dao层接口 -->
<mappers>
<package name="com.dao"/>
</mappers>
</configuration>
spring-dao.xml は、SSM フレームワークの下部、つまりデータベースに接続されている層の関連ファイルの構成に使用されます。
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!-- cj是mysql8.0的一个特性 -->
<property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
spring-mvc.xml は、ビュー パーサーとアノテーションの構成です。つまり、jsp ファイルのプレフィックスとサフィックスの構成、および pojo レイヤー エンティティ クラスのメソッドのエイリアス スキャンの構成であり、メソッドを記述するのに便利です。
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven/>
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- /WEB-INF/jsp/+视图名+.jsp -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.controller"></context:component-scan>
</beans>
spring-service はサービスメソッドのスキャンです
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.service"></context:component-scan>
</beans>
2つ目、ステップの具体的な書き方
1. コントローラー層の方式
@RequestMapping は Web ページの URL を入力します。つまり、ブラウザにhttp://localhost:8080/StudentSSM/pagelistStudentと入力します。ここで StudentSSM はプロジェクト名で、その背後にある pagelistStudent は @ のコンテンツです。
リダイレクトされるページは setViewName のコンテンツであり、これは JSP ページの名前である必要があります (プレフィックスとサフィックスは構成ファイルで設定されているため、ここに JSP ページの名前を入力するだけです)
package com.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.Page;
import com.pojo.Student;
import com.service.StudentService;
@Controller
public class Studentcontroller {
@Autowired
StudentService studentservice;
@RequestMapping("/listStudent")
public ModelAndView listStudent() {
ModelAndView mav = new ModelAndView();
List<Student> stduents=studentservice.list();
mav.addObject("students",stduents);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/pagelistStudent")
public ModelAndView pagelistStudent(Page page) {
ModelAndView mav = new ModelAndView();
int total = studentservice.total();
//设置页码防溢出
if(page.getstart()<0)
page.setstart(0);
if(page.getstart()>total)
page.setstart(total-1);
page.calculatelast(total);
List<Student> pagestudents=studentservice.pagelist(page);
mav.addObject("pagestudents",pagestudents);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/getStudent")
public ModelAndView getStudent(int sno) {
ModelAndView mav = new ModelAndView();
Student student = studentservice.get(sno);
List<Student> get = new ArrayList<Student>();
get.add(student);
mav.addObject("get",get);
mav.setViewName("getStudent");
return mav;
}
@RequestMapping("/deleteStudent")
public ModelAndView deleteStudent(int sno,Page page) {
ModelAndView mav = new ModelAndView();
studentservice.delete(sno);
int total = studentservice.total();
if(page.getstart()<0)
page.setstart(0);
if(page.getstart()>total)
page.setstart(total-1);
page.calculatelast(total);
List<Student> delete=studentservice.pagelist(page);
mav.addObject("delete",delete);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/beforeinsertStudent")
public ModelAndView beforeinsertStudent() {
ModelAndView mav = new ModelAndView();
mav.setViewName("insertStudent");
return mav;
}
@RequestMapping("/insertStudent")
public ModelAndView insertStudent(int sno,String sname,Page page) {
ModelAndView mav = new ModelAndView();
studentservice.insert(sno,sname);
int total = studentservice.total();
if(page.getstart()<0)
page.setstart(0);
if(page.getstart()>total)
page.setstart(total-1);
page.calculatelast(total);
List<Student> insert=studentservice.pagelist(page);
mav.addObject("insert",insert);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("/beforeupdateStudent")
public ModelAndView beforeupdateStudent(int id,int sno,String sname) {
ModelAndView mav = new ModelAndView();
mav.addObject("id",id);
mav.addObject("sno",sno);
mav.addObject("sname",sname);
mav.setViewName("updateStudent");
return mav;
}
@RequestMapping("/updateStudent")
public ModelAndView updateStudent(int id,int sno,String sname) {
ModelAndView mav = new ModelAndView();
studentservice.update(id,sno,sname);
List<Student> update = studentservice.list();
mav.addObject("update",update);
mav.setViewName("listStudent");
return mav;
}
}
2. サービス層方式
Controller層のメソッド呼び出しメソッドとDao層の呼び出しメソッドを実現するインターフェース経由
package com.service;
import java.util.List;
import com.pojo.Page;
import com.pojo.Student;
public interface StudentService {
List<Student> list();
List<Student> pagelist(Page page);
int total();
void delete(int sno);
Student get(int sno);
void insert(int sno,String sname);
void update(int id,int sno,String sname);
}
package com.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.StudentMapper;
import com.pojo.Page;
import com.pojo.Student;
import com.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentmapper;
@Override
public List<Student> list(){
return studentmapper.list();
}
@Override
public Student get(int sno) {
return studentmapper.get(sno);
}
@Override
public void delete(int sno) {
studentmapper.delete(sno);
}
@Override
public void insert(int sno,String sname) {
studentmapper.insert(sno,sname);
}
@Override
public void update(int id,int sno,String sname) {
studentmapper.update(id,sno,sname);
}
@Override
public List<Student> pagelist(Page page) {
// TODO Auto-generated method stub
return studentmapper.pagelist(page);
}
@Override
public int total() {
// TODO Auto-generated method stub
return studentmapper.total();
}
}
3. Daoレイヤー法
Dao 層は基礎となるメソッドを呼び出します
package com.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.pojo.Page;
import com.pojo.Student;
public interface StudentMapper {
public List<Student> list();
public List<Student> pagelist(Page page);
public int total();
public void delete(int sno);
public Student get(int sno);
public void insert(@Param("sno")int sno,@Param("sname")String sname);
public void update(@Param("id")int id,@Param("sno")int sno,@Param("sname")String sname);
}
<?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">
<!-- namespace:命名空间,填写对应接口的全限定类名 -->
<mapper namespace="com.dao.StudentMapper">
<!-- id:list 对应接口的方法-->
<select id="list" resultType="Student">
select * from student
</select>
<select id="pagelist" resultType="Student">
select * from student
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="total" resultType="int">
select count(*) from student
</select>
<select id="get" resultType="Student" parameterType="int">
select * from student where Sno = #{sno}
</select>
<delete id="delete" parameterType="Student">
delete from student where Sno = #{sno}
</delete>
<insert id="insert" parameterType="Student">
insert into student(Sno,Sname) value(#{sno},"${sname}")
</insert>
<update id="update" parameterType="Student">
update student set Sno=#{sno},Sname=#{sname} where id=#{id}
</update>
</mapper>
3、特定のWebページの表示
最終的にはこのようなフォームが表示されますが、名前や学籍番号などは実際とは関係なく、フォーム内のデータとしてのみ使用されます。
要約する
一般に、フレームワークを構築し、関連するメソッドの記述を理解した後は、作業は非常に簡単になり、時間をかけて理解するだけになります。