最简单的ssm整合
防忘大法好
所需jar包
commons-dbcp-1.2.jar
commons-logging-1.1.3.jar
commons-pool-1.6.jar
mybatis-3.4.4.jar
mybatis-spring-1.3.0.jar
mysql-connector-java-5.1.5-bin.jar
spring-aop-4.3.7.RELEASE.jar
spring-beans-4.3.7.RELEASE.jar
spring-context-4.3.7.RELEASE.jar
spring-context-support-4.3.7.RELEASE.jar
spring-core-4.3.7.RELEASE.jar
spring-expression-4.3.7.RELEASE.jar
spring-jdbc-4.3.7.RELEASE.jar
spring-tx-4.3.7.RELEASE.jar
spring-web-4.3.7.RELEASE.jar
spring-webmvc-4.3.7.RELEASE.jar
spring-mvc.xml(该配置文件包含spring,springmvc,mybatis配置)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<!-- auto scan package -->
<context:component-scan base-package="com.dadao.biz"></context:component-scan>
<context:component-scan base-package="com.dadao.controller"></context:component-scan>
<!-- anonotation support -->
<mvc:annotation-driven />
<!-- static file -->
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/image/**" location="/image/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/fonts/**" location="/fonts/" />
<!-- introduce the jdbc config file -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- config mybatis -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- spring integrated with mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/dadao/dao/*.xml" />
</bean>
<!-- map dao and auto scan -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.dadao.dao" />
</bean>
<!-- define the view file -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
jdbc.properties文件如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1/dadao
username=root
password=root
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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>WebDemo</display-name>
<!-- springMVC dispatcher 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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>homePage</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
随便一句sql
<?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.dadao.dao.HomePageDao">
<select id="getMottoByRandom" parameterType="java.lang.String" resultType="com.dadao.po.MottoPO">
SELECT motto FROM tb_app_motto ORDER BY RAND() LIMIT 1
</select>
</mapper>
及其对应的dao
package com.dadao.dao;
import com.dadao.po.MottoPO;
public interface HomePageDao {
public MottoPO getMottoByRandom();
}
对应的biz
package com.dadao.biz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dadao.dao.HomePageDao;
@Service
public class HomePageBiz {
@Autowired
private HomePageDao _dao;
public String getMottoByRandom(){
return _dao.getMottoByRandom().getMotto();
}
}
对应的controller
package com.dadao.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dadao.biz.HomePageBiz;
@Controller
public class HomePageController {
@Autowired
private HomePageBiz _biz;
@RequestMapping(value="homePage")
public String homePage(Model model){
String motto = _biz.getMottoByRandom();
model.addAttribute("motto", motto);
return "index";
}
}
输入http://localhost:8080/WebDemo/homePage
差不多就这样