spring-mybatis.xmljava
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 打開Spring的Annotation支持 --> <context:annotation-config /> <!-- 設定Spring 去哪些包中找Annotation --> <context:component-scan base-package="org.h3c" /> <bean id="dataSource" 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}" /> <!-- 配置鏈接池的初始值 --> <property name="initialSize" value="1" /> <!-- 鏈接池的最大值 --> <!-- <property name="maxActive" value="500"/> --> <!-- 最大空閒時,當通過一個高峯以後,鏈接池能夠將一些用不到的鏈接釋放,一直減小到maxIdle爲止 --> <!-- <property name="maxIdle" value="2"/> --> <!-- 當最小空閒時,當鏈接少於minIdle時會自動去申請一些鏈接 --> <property name="minIdle" value="1" /> <property name="maxActive" value="100" /> <property name="maxIdle" value="20" /> <property name="maxWait" value="1000" /> </bean> <!-- 導入Src目錄下的jdbc.properties文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!--建立Spring的SessionFactory工廠 --> <!-- 若是使用的是Annotation的方式,不能使用LocalSessionFactoryBean,而應該使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- <property name="mapperLocations" value="classpath*:dbmap/*Mapper.xml" /> --> </bean> <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> --> <!-- <property name="basePackage" value="org.h3c.dao" /> --> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> --> <!-- </bean> --> <bean id="IUserDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.h3c.dao.IUserDao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
spring-mvc.xmlweb
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="org.h3c.controller"/> <!-- 將靜態文件指定到某個特殊的文件夾中統一處理 --> <!-- <mvc:default-servlet-handler/> --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 設置multipartResolver才能完成文件上傳 --> <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> --> <!-- <property name="maxUploadSize" value="5000000"></property> --> <!-- </bean> --> <!-- <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> --> <!-- <property name="exceptionMappings"> --> <!-- <props> --> <!-- <prop key="zttc.itat.model.UserException">error</prop> --> <!-- </props> --> <!-- </property> --> <!-- </bean> --> </beans>
web.xmlspring
<?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"> <display-name>SSM</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> --> <param-value>/WEB-INF/spring-mvc.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> <!-- 此處能夠能夠配置成*.do,對應struts的後綴習慣 --> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package org.h3c.service; import java.util.List; import org.h3c.model.User; import org.springframework.stereotype.Repository; @Repository public interface IUserService { public void insertUser(User user); public void updateUser(User user); public void deleteUser(int id); public User selectUser(int id); public List<User> userSelectList(); }
User.javasql
package org.h3c.model; public class User { private int id; private String username; private String password; private String nickname; // private Group group; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, String nickname) { super(); this.username = username; this.password = password; this.nickname = nickname; } public User(int id, String username, String password, String nickname) { super(); this.id = id; this.username = username; this.password = password; this.nickname = nickname; } // public Group getGroup() { // return group; // } // public void setGroup(Group group) { // this.group = group; // } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickName=" + nickname + ", group=" // + group + "]"; } }
IUserService.java(接口)apache
package org.h3c.service; import java.util.List; import org.h3c.model.User; import org.springframework.stereotype.Repository; @Repository public interface IUserService { public void insertUser(User user); public void updateUser(User user); public void deleteUser(int id); public User selectUser(int id); public List<User> userSelectList(); }
UserService.java(實現類)spring-mvc
package org.h3c.service.impl; import java.util.List; import javax.annotation.Resource; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.h3c.dao.IUserDao; import org.h3c.model.User; import org.h3c.service.IUserService; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperFactoryBean; import org.springframework.stereotype.Component; @Component("userService") public class UserService implements IUserService { // @Resource(name="sqlSessionFactory") // private SqlSessionFactoryBean sqlSessionFactory; @Resource(name="IUserDao") //private MapperFactoryBean UserMapper; private IUserDao userDao; @Override public void insertUser(User user) { // TODO Auto-generated method stub } @Override public void updateUser(User user) { // TODO Auto-generated method stub } @Override public void deleteUser(int id) { // TODO Auto-generated method stub } @Override public User selectUser(int id) { try { User u = userDao.selectUser(id); return u; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public List<User> userSelectList() { // TODO Auto-generated method stub return null; } }
IUserDao.javasession
package org.h3c.dao; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.h3c.model.User; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; @Component("userDao") public interface IUserDao { @Insert("INSERT INTO t_user values()") public void insertUser(User user); public void updateUser(User user); public void deleteUser(int id); @Select("SELECT * FROM t_user where id=#{id}") public User selectUser(int id); public List<User> userSelectList(); }
UserController.javamybatis
package org.h3c.controller; import javax.annotation.Resource; import org.h3c.model.User; import org.h3c.service.IUserService; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class UserController { @Resource(name="userService") private IUserService userService; @RequestMapping("/getuser") public String getUser(int id,Model model){ System.out.println(id); User u =userService.selectUser(id); model.addAttribute(u); System.out.println(u); return "user/user"; } public void setUserService(IUserService userService) { this.userService = userService; } }
package org.h3c.service.impl; import java.util.List; import javax.annotation.Resource; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.h3c.dao.IUserDao; import org.h3c.model.User; import org.h3c.service.IUserService; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperFactoryBean; import org.springframework.stereotype.Component; @Component("userService") public class UserService implements IUserService { // @Resource(name="sqlSessionFactory") // private SqlSessionFactoryBean sqlSessionFactory; @Resource(name="IUserDao") //private MapperFactoryBean UserMapper; private IUserDao userDao; @Override public void insertUser(User user) { // TODO Auto-generated method stub } @Override public void updateUser(User user) { // TODO Auto-generated method stub } @Override public void deleteUser(int id) { // TODO Auto-generated method stub } @Override public User selectUser(int id) { try { User u = userDao.selectUser(id); return u; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public List<User> userSelectList() { // TODO Auto-generated method stub return null; } }
整個項目目錄結構以下:mvc
若是沒有jstl的包,那麼咱們的jsp頁面就不能使用${}app