SSM integration practice

Keywords: Spring xml Mybatis encoding

Management dao layer of mybatis framework
spring mainly manages the service layer
Spring MVC manages the cortroller layer
1. guide pack


2. Write web.xml. When Tomcat starts, load the web.xml file and put its contents into the spring container (webApplicationContext, which is the sub interface of applicationContext).

<?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_3_1.xsd" 
 id="WebApp_ID" version="3.1">
	<!-- Set up spring Location and name of the profile -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- Listener loading spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- Character encoding filter --> 
	<filter> 
		<filter-name>encoding</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>encoding</filter-name> 
		<url-pattern>/*</url-pattern> 
	</filter-mapping>
	
	<!-- To configure springMVC -->
	<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>		
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc</servlet-name>
		<!-- All requests in springMVC(except jsp file) -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

3. Write entity class

package com.boke.pojo;
public class User {
	private int id;
	private String name;
	private String password;
	private String statue;
	private String type;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getStatue() {
		return statue;
	}
	public void setStatue(String statue) {
		this.statue = statue;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + ", statue=" + statue + ", type=" + type
				+ "]";
	}	
}

Now let's integrate spring framework and mybatis framework
4. Write the configuration file applicationContext.xml of mybatis

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!--Configure data sources-->
    <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/daily"></property>
   		<property name="username" value="root"></property>
   		<property name="password" value="1234"></property>
   </bean>
  <!--generate session factory-->
   <bean id="util" class="org.mybatis.spring.SqlSessionFactoryBean">
   		<property name="dataSource" ref="dataSource"></property>	
   </bean>
  <!--Mapping class-->
   <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   		<property name="basePackage" value="com.boke.mapper"></property>
   		<property name="sqlSessionFactory" ref="util"></property>
   </bean>   
   <!-- Annotation configuration service  aop -->
	<context:component-scan base-package="com.boke.serviceimpl"></context:component-scan>
	
</beans>

5. Write dao layer / mapper layer (annotation method)

public interface UserMapper {
	@Select("select * from user")
	List<User> selall();
	@Insert("insert into user value(default,#{name},#{statue},#{password},#{type})")
	int insuser(User u);
	@Delete("delete from user where id=#{0}")
	int deluser(int id);
}

I'll match mybatis here

6. Write the service layer configuration file springservice.xml, mainly for declarative transaction processing and notification

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- Annotation configuration service  aop Because in applicationContext,xml It's configured, so it's not needed here-->
	<!--  
		<context:component-scan base-package="com.boke.service"></context:component-scan>
	-->
	<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- Configure declarative transactions -->
	<tx:advice id="txadvice" transaction-manager="txmanager">
		<tx:attributes>
			<tx:method name="ins*"/>
			<tx:method name="del*"/>
			<tx:method name="selall" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!--section  -->
	<aop:config>
		<!--Tangent point  -->
		<aop:pointcut expression="execution(* com.boke.serviceimpl.*.*(..))" id="mypoint"/>
		<!--notice -->
		<aop:advisor advice-ref="txadvice" pointcut-ref="mypoint"/>
	</aop:config>
</beans>

Write service layer

7. Service layer interface:

public interface UserService {
	List<User> selall();
	
	int insuser(User u);
	
	int deluser(int id);
	void login();
}

8. Service layer implementation

package com.boke.serviceimpl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boke.mapper.UserMapper;
import com.boke.pojo.User;
import com.boke.service.UserService;
//@Service("userService") is used to replace the < bean id = "" class = "" > < bean > of the configuration file. It is used to instantiate this class. The created object of this class is called userService,
@Service("userService")
public class UserServiceImpl implements UserService {
	//@Resource is used to map automatically through byName. If byName does not match, bytype
	@Resource
	private UserMapper userMapper;
	@Override
	public List<User> selall() {
		return userMapper.selall();
	}
	public void login(){
		System.out.println("login Ha ha ha");
	}
	@Override
	public int insuser(User u) {
		return userMapper.insuser(u);
	}
	@Override
	public int deluser(int id) {
		return userMapper.deluser(id);
	}
}

Now that we have integrated spring and mybatis, let's add the spring MVC framework
9. Write springmvc.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<!-- Scan controller notes under the package -->
	<context:component-scan base-package="com.boke.controller">
	</context:component-scan>
	
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- Static configuration -->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources> 
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources> 
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
</beans>

10. Write controller layer

package com.boke.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boke.pojo.User;
import com.boke.service.UserService;
//@The Controller is equivalent to the @ service function of the service layer
@Controller
public class ControllerTest {
	@Resource
	private UserService userService;
	@ResponseBody
	@RequestMapping("test")
	public String test(){
		System.out.println("Ha ha ha ha");
		userService.login();
		List<User> list = userService.selall();
		System.out.println(list);
		return list.toString();
	}
}

The integration of ssm framework is finished here
The configuration process encountered a version mismatch.

The initial mybatis-3.5.jar. Does not match with mybatis-spring-1.2.3.jar. It has been reported that
Exception - java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout exception.

Published 14 original articles, won praise 8, visited 4710
Private letter follow

Posted by jim.davidson on Wed, 12 Feb 2020 07:58:06 -0800