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。希望能對你有所幫助。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章