Spring MVC整合Mybatis实例
本文基于Spring 注解,让Spring跑起来。本文使用Mysql数据库。
(1) 导入相关包,包结构如下图所示:
(2) 修改src/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:p="http://www.springframework.org/schema/p"
- 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/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="classpath:jdbc.properties" />
- <!--创建jdbc数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- </bean>
- <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 可通过注解控制事务 -->
- <tx:annotation-driven />
- <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.geloin.spring.mapper" />
- </bean>
- </beans>
(3) 在src下添加jdbc.properties
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/ruisystem
- username=root
- password=root
(4) 在com.geloin.spring.entity包下添加实体类,实体类对应于数据表,其属性与数据表相同或多于数据表。
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:24:43
- */
- package com.geloin.spring.entity;
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:24:43
- */
- public class Menu {
- /**
- * 惟一标识
- */
- private Integer id;
- /**
- * 父ID
- */
- private Integer parentId;
- /**
- * 名称
- */
- private String name;
- /**
- * 对应的地址
- */
- private String url;
- /**
- * 是否显示在左侧
- */
- private Integer isShowLeft;
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @return the id
- */
- public Integer getId() {
- return id;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @param id
- * the id to set
- */
- public void setId(Integer id) {
- this.id = id;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @return the parentId
- */
- public Integer getParentId() {
- return parentId;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @param parentId
- * the parentId to set
- */
- public void setParentId(Integer parentId) {
- this.parentId = parentId;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @param name
- * the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @return the url
- */
- public String getUrl() {
- return url;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @param url
- * the url to set
- */
- public void setUrl(String url) {
- this.url = url;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @return the isShowLeft
- */
- public Integer getIsShowLeft() {
- return isShowLeft;
- }
- /**
- *
- * @author geloin
- * @date 2012-5-5 上午10:26:19
- * @param isShowLeft
- * the isShowLeft to set
- */
- public void setIsShowLeft(Integer isShowLeft) {
- this.isShowLeft = isShowLeft;
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20