SSM integration, spring-mybatis configuration, service, controller configuration

    SSM, the abbreviation of spring, springmvc, mybatis, here, I will give you a brief description of the following places to be configured.

1. First of all, we need to create the corresponding package and determine the location where each type of file should be stored.

2. Import the corresponding shelf package. I will put the specific shelf package in WebContent/WEB-INF/lib and I will upload the simple SSM framework I integrated later.

3. Write the spring-mybatis.xml file, which is the file that integrates spring and 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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- Import properties file -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
id="propertyConfigurer">
<property value="classpath:config/jdbc.properties" name="location" /> <!-- The properties file is written by myself and contains various properties required by the configuration database below. You can also skip this step and directly put The various attributes of the database are written in the corresponding positions, which are similar to what we usually need to use to connect to the database with code -->
</bean>
<!-- Configure database data source -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Initialize connection size -->
<property name="initialSize" value="${initialSize}"></property>
<!-- Maximum number of connection pools-->
<property name="maxActive" value="${maxActive}"></property>
<!-- Maximum idle connection pool -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- Connection pool minimum idle-->
<property name="minIdle" value="${minIdle}"></property>
<!-- Get the maximum wait time for connection-->
<property name="maxWait" value="${maxWait}"></property>
</bean>


<!-- spring integrates MyBatis data source and mapping file-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds" />
<!-- Automatically scan the mapping.xml file-->
<property name="mapperLocations" value="classpath:com/test/mapping/*.xml" /> <!-- The only thing we need to configure here is the following value, which is the location of our own mapping file, classpath: is equivalent to src in the project  -->
</bean>


<!-- The package name where the dao interface is located, Spring will automatically find the class under it -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.dao" /> <!-- The only thing we need to configure here is the following value, which is the interface file path when we integrate mybatis  -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>


<!-- (transaction management) transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds" />
</bean>
</beans>

All the above places that we need to modify, I have commented out.

4. After the above steps are completed, we can start to write our project. Like mybatis, we must first write the corresponding mapping.xml file, and then write the interface to realize the desired database function.

The following is the xml file and interface I wrote casually

<?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.test.dao.TeacherDao">
<select id="TeaCount" resultType="java.lang.Integer"><!-- The id here needs to be used in the interface, so try to write something that you can understand. The return type behind is the type of our query result , here we query the quantity, so the return value type is Integer, and the query below is the teacher, so the return type is the instance class of our corresponding teacher in the com.test.po package  -->
select count(id) from teacher;
</select>
<select id="selectAll" resultType="com.test.po.Teacher">
select id,name,age,sex from teacher
</select>
</mapper>

下面是我写的接口

package com.test.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.test.po.Teacher;

public interface TeacherDao {
      int TeaCount();   //方法名就是上面映射文件中对应的id值。
      List<Teacher> selectAll();
}

五、在搞定mybatis之后,我们要编写对应的service层。

首先我们要编写整合service的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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包 -->
<mvc:annotation-driven />
<!-- 配置控制器的包 -->
<context:component-scan base-package="com.test.service" /><!-- 这里我配置的是包,它会自动扫描com.test.service包下的所有文件 -->
</beans>

然后我们就可以编写service层了,我们需要创建一个接口跟一个实现类

package com.test.service;
import java.util.List;
import com.test.po.Teacher;


public interface Teacherservicesupport {
     public int countofPage();
     List<Teacher> selectAllt();
}


package com.test.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.dao.TeacherDao;
import com.test.po.Teacher;

@Service 
public class Teacherservice implements Teacherservicesupport{
    @Autowired
    private TeacherDao dao;
@Override
public int countofPage() {
//具体的方法
}


@Override
public List<Teacher> selectAllt() {
List<Teacher> list=dao.selectAll();
return list;
}
}

六、编写我们的控制层

首先,我们要做的依然是编写控制层的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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包 -->
<mvc:annotation-driven />
<!-- 配置控制器的包 -->
<context:component-scan base-package="com.test.controller" />
<bean id="ViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean><!-- 视图解析器,我们的controller中可能含有普通的servlet,需要用来解析view,这里也可以不要这个 -->
</beans>

然后编写控制层文件

package com.test.controller;
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.ResponseBody;
import com.test.service.Teacherservicesupport;


@Controller
public class TeacherController {
@Autowired
private Teacherservicesupport support;
@RequestMapping("getList.do")//声明访问的路径
@ResponseBody//表明为ajax请求
public String test1(){
     return null;//根据自己的需要编写实际的函数
}
}


七、编写我们的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"
id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>Tclass.jsp</welcome-file>
</welcome-file-list>

<!-- 设置mybatis和service的配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mybatis.xml,classpath:config/spring-service.xml</param-value><!-- 这里配置的是前面的配置文件的路径 -->
</context-param>

<!-- Spring监听器项目加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring MVC DispatcherServlet -->
<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:config/spring-controller.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>*.do</url-pattern>
</servlet-mapping>

<!-- LOG4J的配置文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
<!-- 定义LOG4J监听器 -->   
<listener>   
  <listener-class>   
org.springframework.web.util.Log4jConfigListener   
  </listener-class>   
</listener> 

<!-- 编码过滤器 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>

以上全部完成后,整个框架就完成了,我们就可以在对应的包中编写代码,实现想要的功能了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325612646&siteId=291194637