spring+springmvc+mybatis整合
来源:互联网 发布:淘宝联盟的优惠券在哪 编辑:程序博客网 时间:2024/05/18 09:49
整合spring+springmvc+mybatis
1.新建web项目
2.导入jar包
aopalliance.jaraspectjweaver.jarcommons-logging-1.1.1.jarmybatis-3.2.7.jarmybatis-spring-1.2.3.jarmysql-connector-java-5.1.20-bin.jarspring-aop-4.1.6.RELEASE.jarspring-aspects-4.1.6.RELEASE.jarspring-beans-4.1.6.RELEASE.jarspring-context-4.1.6.RELEASE.jarspring-core-4.1.6.RELEASE.jarspring-expression-4.1.6.RELEASE.jarspring-jdbc-4.1.6.RELEASE.jarspring-tx-4.1.6.RELEASE.jarspring-web-4.1.6.RELEASE.jarspring-webmvc-4.1.6.RELEASE.jar
3.新建vo类
User.java
package cn.zwq.vo;public class User { private int id; private String username; private String password; 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; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; }}
4.新建Mapper
UserMaper.interface
public interface UserMapper { public List<User> findAll();}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.zwq.mapper.UserMapper"> <select id="findAll" resultType="cn.zwq.vo.User"> select * from t_user </select></mapper>
5.新建service层
UserService.java
public interface UserService { public List<User> findAll();}
UserServiceImpl.java
@Service("userService")public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public List<User> findAll() { return userMapper.findAll(); }}
6.新建控制器
UserController.java
@Controllerpublic class UserController { @Autowired private UserService userService; @RequestMapping("/list") public ModelAndView list(){ ModelAndView mv = new ModelAndView("list"); mv.addObject("list", userService.findAll()); return mv; }}
7.spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- <property name="configLocation" value="classpath:mybatis.cfg.xml"></property> --> <property name="mapperLocations"> <list> <value>classpath:cn/zwq/mapper/UserMapper.xml</value> </list> </property> </bean> <!-- 自动扫描包,并且生成mapper的实现类,生成的bean的名称为mapper接口的首字母小写 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.zwq.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 配置声明式事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务aop --> <aop:config> <aop:pointcut expression="execution(* cn.zwq.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <context:component-scan base-package="cn.zwq"/></beans>
8.配置springmvc配置文件
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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.xsd"> <!-- 注解开发适配器 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 为响应的视图名称加上前缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 为响应的视图名称加上后缀 --> <property name="suffix" value=".jsp"/> </bean> <!-- 扫描注解类 --> <context:component-scan base-package="cn.zwq.controller"/></beans>
9.配置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="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 乱码解决 --> <filter> <filter-name>CharacterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 防止静态文件被拦截 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <!-- spring mvc --> <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:mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
10.编写jsp页面
–使用jstl不要忘记导入标签。
<body> <table> <tr> <td>id</td> <td>name</td> <td>pwd</td> </tr> <c:forEach items="${list}" var="bean"> <tr> <td>${bean.id}</td> <td>${bean.username}</td> <td>${bean.password}</td> </tr> </c:forEach> </table> </body>
2 0
- Spring-SpringMVC-Mybatis整合
- springmvc+spring+mybatis整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- springmvc+mybatis+spring整合
- springmvc+spring+mybatis 整合
- mybatis+springmvc+spring 整合
- SpringMVC+Spring+Mybatis整合
- springmvc+spring+mybatis 整合
- springmvc+spring+mybatis整合
- Spring+Springmvc+MyBatis整合
- Spring+Springmvc+MyBatis整合
- Spring+Springmvc+MyBatis整合
- springmvc+mybatis+spring 整合
- springmvc+mybatis+spring 整合
- spring+springMVC+Mybatis整合
- spring springmvc mybatis 整合
- springmvc+mybatis+spring整合
- C++判断ip是否为保留ip
- JFace中Dialog类的使用方法
- JAVA使用jsoup技术实现网站URL解析爬取|爬取网站登陆后页面动态数据
- expdp与impdp参数用法
- R语言 柱状图与折线图 趋势图 highcharter (hc_yAxis_multiples)图
- spring+springmvc+mybatis整合
- mysql之数据处理函数与数据汇总函数
- Java 基础知识总结
- Yii2框架bootstrap样式理解
- nyoj 关于521
- 无需设置viewholder的Listview写法
- Rxlifecycle 中提供了 RxAppCompatActivity
- 程序员职业素养
- 纹理下载到内存