第十一章 SSM框架整合

搭建框架

       1. 使用spring整合mybatis,springmvc:引入支持,依賴jar

       2. 創建mybatis-config.xml;數據庫操作

<?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>
		<!-- mybatis:連接數據庫,由spring進行統一管理 -->
</configuration>

       3. 創建applicationContext.xml;spring整合mybatis

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	">
	
	<!-- 配置Service層掃描包 -->
	<context:component-scan base-package="com.dao"></context:component-scan>
	<context:component-scan base-package="com.service"></context:component-scan>
	
	<!-- 配置數據源:數據庫連接信息 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <!-- 解決數據庫亂碼,'&'進行轉義 -->
		<property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=UTF-8"></property>
		<property name="username" value="root" ></property>
		<property name="password" value=""></property>
	</bean>
	
	<!-- 配置sqlSessionFactory:mybatis實現對數據庫操作 -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 設置數據源參考:加載數據庫連接信息 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 掃描數據庫接口映射文件mapper.xml -->
		<property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
		<!-- 引入mybatis配置文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	</bean>
	
	<!-- DAO層操作數據庫接口所在包 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.dao"></property>
		<!-- 指定該包下接口操作對應的SqlSessionFactory -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
	</bean>
		
	
	<!-- 注入事務管理類bean -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置事務通知屬性:由指定bean進行事務管理 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 定義事務傳播屬性 -->
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
			<tx:method name="*" propagation="REQUIRED" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置事務的切入點 -->
	<aop:config>
		<!-- 指定包下任意方法進行事務管理 -->
		<aop:pointcut expression="execution(* com.service.*.*(..))" id="servicePoint"/>
		<!-- 事務與方法建立聯繫 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePoint"/>
	</aop:config>
	
</beans>

       4. 創建springmvc.xml,整合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: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-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	">
	<!-- 配置掃描包 -->
	<context:component-scan base-package="com.controller"/>
	
	<!-- 配置註解驅動:可以不用配置處理器映射器和處理器適配器 -->
	<mvc:annotation-driven/>
	
	<!-- 配置視圖解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

       5. 配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.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:springmvc.xml</param-value>
    </init-param>
  </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:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 中文亂碼過濾器 -->
  <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>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

測試

package com.model;

/*
 * 實體類:與數據庫表對應
 */
public class Student {
	private Integer stuno = null;
	private String sname;
	private int age;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(Integer stuno, String sname, int age) {
		super();
		this.stuno = stuno;
		this.sname = sname;
		this.age = age;
	}
	public Integer getStuno() {
		return stuno;
	}
	public void setStuno(Integer stuno) {
		this.stuno = stuno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [stuno=" + stuno + ", sname=" + sname + ", age=" + age
				+ "]";
	}
}
package com.dao;

import com.model.Student;
/*
 * 	dao層處理數據庫接口
 */
public interface StudentDao {
	public int insert(Student stu);
}
<?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.dao.StudentDao">
<!-- 映射文件 -->
	<insert id="insert" parameterType="com.model.Student">
		insert into student values(null,#{sname},#{age})
	</insert>
</mapper>
package com.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dao.StudentDao;
import com.model.Student;

/*
 * 業務層:加工dao層數據
 */
// 使用註解指明service層
@Service
public class StudentService {
	// 使用Resource註解,替代在spring容器中注入javabean
	@Resource
	private StudentDao studentDao;
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
	
	public boolean insert(Student stu){	
		int i = studentDao.insert(stu);
		if(i>0){
			return true;
		}
		return false;
	}
}
package com.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.Student;
import com.service.StudentService;
/*
 * 控制層:訪問該路徑可以將學生信息寫入數據庫
 */
@Controller
@RequestMapping("/stu")
public class StudentController {
	@Resource
	private StudentService studentService;	
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	
	@RequestMapping("/insert.do")
	public String insert(){
		Student stu = new Student(null, "墨漸生微", 19);
		boolean flag = studentService.insert(stu);
		System.out.println(flag);
		return "index";	
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章