最简单的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配置)
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">
jdbc.properties文件如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1/dadao
username=root
password=root
web.xml文件如下
随便一句sql
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
及其对应的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
差不多就这样