SSM框架開發web專案系列(一) 環境搭建篇
阿新 • • 發佈:2019-01-02
前言
開發環境:Eclipse Mars + Maven + JDK 1.7 + Tomcat 7 + MySQL
主要框架:Spring + Spring MVC + Mybatis
目的:快速上手搭建SSM開發環境,熟悉客戶端請求到資料庫操作的流程。
準備工作
本文以“人”Person物件為例,所以首先在資料庫中建一張表。這裡用的Navicat for MySql,新建一個名為ssm的資料庫,然後在庫中新建一張名為TBL_PERSON的表,3個欄位,分別為主鍵id,姓名name,性別gender,都為字串型別。這裡主鍵id沒有采用自增,而是用UUID隨機字串。資料表概要如圖所示。
新建一個maven web專案,工程結構如下,並建立一個pojo類Person
package com.mmm.pojo; /** * Person實體類 * */ public class Person { private String id; private String name; private String gender; public String getId() { return id; } public void setId(String id) {this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
配置篇
1.Spring 整合Mybatis配置檔案以及應用(application-root.xml + maybatis-config.xml + jsbc.properties)
<?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:p="http://www.springframework.org/schema/p" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 引入資料庫配置引數檔案jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- Spring會掃描指定包下的bean註解例如@Controller、@Service等 --> <context:component-scan base-package="com.mmm" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- spring和MyBatis整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapper.xml檔案 --> <property name="mapperLocations" value="classpath:com/mmm/mapper/*.xml"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- Mapper介面所在包名,Spring會自動查詢其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mmm.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
<?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> <!-- 這裡可以定義類的別名,在mapper.xml檔案中應用會方便很多 --> <typeAliases> <typeAlias alias="person" type="com.mmm.pojo.Person" /> </typeAliases> </configuration>
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
2.Spring MVC配置(application-mvc.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:p="http://www.springframework.org/schema/p" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動掃描開啟 --> <context:component-scan base-package="com.mmm.web" /> <mvc:annotation-driven/> <!-- Spring MVC檢視解析配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
3.PersonMapper對映配置檔案(personMapper.xml)
<?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.mmm.mapper.PersonMapper"> <resultMap type="person" id="personResultMap" > <id property="id" column="id"></id> <id property="name" column="name"></id> <id property="gender" column="gender"></id> </resultMap> <!-- 新增一條記錄 --> <insert id="insert" parameterType="person" > <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select replace(uuid(),'-','') from dual </selectKey> insert into `TBL_PERSON` values (#{id},#{name},#{gender}) </insert> <!-- 刪除一條記錄 --> <delete id="delete"> delete from `TBL_PERSON` where id = #{id} </delete> <!-- 更新一條記錄 --> <update id="update" parameterType="person"> update `TBL_PERSON` set name = #{name}, gender = #{gender} </update> <!-- 查詢所有記錄 --> <select id="selectAll" resultMap="personResultMap"> select * from `TBL_PERSON` </select> <!-- 根據主鍵id查詢記錄 --> <select id="selectById" resultType="person"> select * from `TBL_PERSON` where id = #{id} </select> </mapper>
4.web.xml配置(web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- Spring的配置檔案 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-root.xml</param-value> </context-param> <!-- 編碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
程式碼篇
1.mapper包下(com.mmm.mapper)
package com.mmm.mapper; import java.util.List; import com.mmm.pojo.Person; public interface PersonMapper { //新增一個Person物件 void insert(Person p); //根據主鍵id刪除Person物件 void deleteById(String id); //修改一個person物件 void update(Person p); //根據主鍵id查詢Person物件 Person selectById(String id); //查詢所有Person物件,返回集合型別 List<Person> selectAll(); }
2.service包下(com.mmm.service)
package com.mmm.service; import java.util.List; import com.mmm.pojo.Person; public interface PersonService { //新增一個Person物件 void insert(Person p); //根據主鍵id刪除Person物件 void deleteById(String id); //修改一個person物件 void update(Person p); //根據主鍵id查詢Person物件 Person selectById(String id); //查詢所有Person物件,返回集合型別 List<Person> selectAll(); }
package com.mmm.service; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.mmm.mapper.PersonMapper; import com.mmm.pojo.Person; @Service public class PersonServiceImpl implements PersonService { @Resource PersonMapper personMapper; @Override public void insert(Person p) { personMapper.insert(p); } @Override public void deleteById(String id) { personMapper.deleteById(id); } @Override public void update(Person p) { personMapper.update(p); } @Override public Person selectById(String id) { return personMapper.selectById(id); } @Override public List<Person> selectAll() { return personMapper.selectAll(); } }
3.web包下(com.mmm.web)
package com.mmm.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.mmm.pojo.Person; import com.mmm.service.PersonService; @Controller @RequestMapping("person") public class PersonController { @Autowired PersonService personService; /** * 這裡return的字串"mvc"加上前面Spring MVC配置檔案中檢視解析配置的字首"/WEB-INF/views/"和字尾".jsp", * 構成了要請求轉發要跳轉到的頁面路徑,從根路徑webapp下開始,即為/WEB-INF/views/mvc.jsp * */ @RequestMapping(value="mvc") public String testMvc() { return "mvc";// } /** * 這裡@ResponseBody代表方法是直接返回一個json資料回前端, * 注意Spring mvc配置