SSM--SSM整合
一、项目层级结构以及所需JAR包:
二、SpringMVC、Spring、Mybatis核心配置文件:
1.Mybatis:sqMapConfig.xml
2.Spring:applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 数据源dataSource sqlMapConfig.xml -->
<!-- 加载db.properties文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置mybatis sqlSessionFactory -->
<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 告诉spring mybatis核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!-- mapper动态扫描开发 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.skh.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean name="userService" class="cn.skh.service.impl.UserServiceImpl">
<property name="um" ref="userMapper"></property>
<!-- userMapper就为UseMapper的对象 通过mapper动态扫描开发配置好了直接引用即可 -->
</bean>
</beans>
3.SpringMVC:applicationContext-controller.xml:
三、Mapper、Service、Controller
1.mapper:
3.Service以及实现类:
2.Controller:
package cn.skh.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.skh.bean.User;
import cn.skh.service.UserService;
@Controller
@RequestMapping("uc")
public class UserController {
@Autowired
private UserService us ;
@RequestMapping("selectAll")
public ModelAndView select() {
ModelAndView mv = new ModelAndView("index");
List<User> lists = us.selectAllUser();
mv.addObject("list", lists);//放入request域中
return mv;
}
@RequestMapping("delete/{id}")
public String deleteUser(@PathVariable("id") int id,Map<String,String>map) {
us.deleteUser(id);
map.put("type", "delete");
return "success";
}
@RequestMapping("update")
public String updateUser(User u,Map<String,String> map) {
us.updateById(u);
map.put("type", "update");
return "success";
}
@RequestMapping("add")
public String addUser(User u,Map<String,String>map) {
us.insertOne(u);
map.put("type", "add");
return "success";
}
}
四、总结:
在这里实现了基本的增删改查,在整合ssm的时一定要注意Jar包的版本号,不然无平白无故多出很多问题,还有使用SpringMVC时别忘了配置web.xml,让所有的请求归SpringMVC管,而不再是Servlet。希望能对你有所帮助。
还没有评论,来说两句吧...