码迷,mamicode.com
首页 > 编程语言 > 详细

Spring-SpringMVC-Mybatis整合

时间:2014-09-10 22:31:57      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   java   ar   

项目截图

bubuko.com,布布扣

项目所需要的包

bubuko.com,布布扣bubuko.com,布布扣

项目的配置文件

web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>json_test</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>


<!-- 加载所有的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-*.xml</param-value>
</context-param>


<!-- 配置Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- 配置SpringMVC -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/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>


<!-- 配置字符集 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>



spring-common.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


<!--配置数据源 -->
<!-- 配置数据源 -->
<bean id="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>






<!--配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="typeAliasesPackage" value="com.iss.model"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>


<!-- MappperScannerConfigure:扫描特定的包自动创建成批地映射器。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.iss.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>


</bean>




<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jdbcDataSource" />
</bean>


<!--配置事务处理策略,transaction-manager属性指定事务管理器。 若事务管理器bean的id即为transactionManager,则 
transaction-manager的属性可以不指定 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--所有以find开头的方法都是只读的 -->
<tx:method name="get*" read-only="true" />
<tx:method name="add*" />
<!--其他方法使用默认事务策略 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
<!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配com.abc.dao包下的所有类的所有 
方法 -->
<aop:pointcut id="myPointcut" expression="execution(* com.iss.dao.*.*(..))" />
<!--将定义好的事务处理策略应用到上述的切入点 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>


</beans>


spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


<!-- 注解扫描包 -->
<context:component-scan base-package="com.iss" />


<!-- 开启注解 -->
<mvc:annotation-driven />


<!-- 静态资源(js/image)的访问 -->
<mvc:resources location="/js/" mapping="/js/**" />


<!-- 定义视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>



mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
    "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 把数据源交给Spring管理 -->
<!-- <mappers>
<mapper resource="com/iss/dao/UserMapper.xml" />
</mappers> -->
</configuration>



User.java

package com.iss.model;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


/**
 * @author dell
 * 
 */
@Entity
@Table(name = "user")
public class User {
private int id;


private String name;


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


}


UserMapper.java

package com.iss.mapper;


import java.util.List;


import com.iss.model.User;


public interface UserMapper {
public List<User> getAllUsers();


}



UserMapper.xml

<?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.iss.mapper.UserMapper">
<resultMap type="User" id="simpleUserResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>


<select id="getAllUsers" resultMap="simpleUserResultMap">
select * from user;
</select>

</mapper>



UserDao.java

package com.iss.dao;


import java.util.List;


import com.iss.model.User;


public interface UserDao {


public List<User> getAllUsers();


}



UserDaoImpl.java

package com.iss.dao.impl;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import com.iss.dao.UserDao;
import com.iss.mapper.UserMapper;
import com.iss.model.User;


@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private UserMapper userMapper;


public List<User> getAllUsers() {
List<User> users = userMapper.getAllUsers();
return users;
}


}



UserService.java

package com.iss.service;


import java.util.List;


import com.iss.model.User;


public interface UserService {




public List<User> getAllUsers();


}



UserServiceImpl.java

package com.iss.service.impl;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.iss.dao.UserDao;
import com.iss.model.User;
import com.iss.service.UserService;


@Service("UserService")
public class UserServiceImpl implements UserService {


private UserDao userDao;


@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}


public List<User> getAllUsers() {
// TODO Auto-generated method stub
return userDao.getAllUsers();
}


}



UserController.java

package com.iss.controller;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import com.iss.model.User;
import com.iss.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {


private UserService userService;


@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}



@RequestMapping("/getAllUser")
public String getAllUsers(ModelMap modelMap) {
List<User> userList = userService.getAllUsers();
modelMap.addAttribute("userList", userList);
return "index";


}


}


index.jsp

<table border="1">
<tbody>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:if test="${!empty userList }">
<c:forEach items="${userList }" var="user">
<tr id="tr${user.id }">
<td>${user.id }</td>
<td>${user.name}</td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>

完成之后将项目部署到tomcat输入路径即可访问index,jsp页面 博主是初学又不懂之处可以多多交流!

部署路径:http://localhost:8080/projectName/user/getAllUser/



Spring-SpringMVC-Mybatis整合

标签:style   blog   http   color   io   os   使用   java   ar   

原文地址:http://blog.csdn.net/wangdianyong/article/details/39187103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!