Spring-SpringMVC-Mybatis整合

项目截图

项目所需要的包

项目的配置文件

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/

时间: 2025-01-17 16:15:50

Spring-SpringMVC-Mybatis整合的相关文章

Spring+SpringMVC +MyBatis整合配置文件案例66666

Spring+SpringMVC +MyBatis整合配置文件案例 标签: springspringmvcmybatismvcjava 2017-04-13 19:12 228人阅读 评论(1) 收藏 举报 分类: java_javaSE(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Spring+SpringMVC +MyBatis整合配置文件案例 针对spring/SpringMVC/MyBatis三个框架的整合有很多的方式,经过最近的学习我来总结一下其配置文

ssm之spring+springmvc+mybatis整合初探

1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?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/

spring+springMVC+mybatis整合并使用redis做缓存

1.用idea 构建一个maven 的web项目 点击下一步 继续下一步 选着maven home directory 自己的maven 安装目录,全选下一步,等一分钟就OK了 2.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

Spring+SpringMvc+Mybatis整合注意事项

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" xsi:schemaLocation="http://java.sun.com/xml/n

2016.5.19 SSM 框架(一) Spring SpringMVC Mybatis 整合思路

前言 : 内容 依然是根据 传智播客 燕青老师 视频 及自己理解所写 1. 总体框架图 2.理解 可能从后往前更好理解一点 底层: 数据库    mysql  sqlserver oracle 等等 持久层(DAO):                   ---调用数据库   提供mapper接口 持久层 生成 Mapper接口(DAO接口), 提供访问数据库的方法和数据. 业务层(Service):                --- 调用 mapper 接口     提供Service接口

Spring+SpringMVC+MyBatis整合应用

1)搭建Spring,SpringMVC和MyBatis环境 创建一个web工程 添加MyBatis相关环境 引入数据库驱动包和DBCP连接池开发包 引入MyBatis开发包 添加Spring,SpringMVC相关技术环境 引入Spring ioc,jdbc-tx,aop开发包 引入Spring web,webmvc开发包 在src下添加spring.xml配置文件 在web.xml中配置DispatcherServlet前端控制器和中文乱码 处理过滤器CharacterEncodingFil

Spring+SpringMVC+Mybatis整合 pom示例

<!-- SpringMVC,Spring --> <dependencies> <!-- Spring mvc --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spri

Spring+SpringMVC+mybatis整合以及注解的使用

1.包结构:  2.spring配置:基本的DAO配置以及扫描Mapper(扫描出来的Mapper为首字母小写) 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/sche

Spring SpringMvc + MyBatis 整合

一.使用的jar包就不详细讲解了,下载了Mybatis 和 Spring 的jar包基本上都添加上去了. 一图概括:(这是我使用的ar包,有些不是Mybatis 和 Spring 的 )  二. web.xml配置文件 [html] view plaincopy <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSche

Spring+SpringMVC+MyBatis整合配置

前端控制器 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="