这段时间没更博,找房去了。。。   吐槽一下,自如太坑了。。。承诺的三年不涨房租,结果今年一看北京房租都在涨也跟着涨了。。。 而且自如太贵了,租不起了。。 突然有点理解女生找对象要房了。。   搬家太受罪了。。。

今天更一下springMVC整合mybits形成最简单的网站demo。

大概效果就是这样的:左边是数据库查询结果,右边是页面访问结果:

首先,一个简单的springMVC小例子可以看这个http://www.cnblogs.com/xuejupo/p/5236448.html

这是在这个基础上增加了mybits的配置和对数据库的访问。

在pom文件里增加对mybits的支持和druid数据源包:

<!-- mybits的基本依赖开始 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
        <!-- mybits的基本依赖结束 -->
        <!--第三方依赖包开始 -->
        <!-- 这是阿里巴巴的druid数据源依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>0.2.20</version>
        </dependency>

配置mybits的配置文件

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.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
">

    <context:component-scan base-package="controller"></context:component-scan>
    <context:component-scan base-package="com.cainiaojava"></context:component-scan>
    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 配置数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="initialSize" value="1" />
        <property name="maxActive" value="20" />
        <property name="minIdle" value="1" />
        <property name="maxWait" value="60000" />
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="1800" />
        <property name="logAbandoned" value="true" />
        <property name="filters" value="mergeStat" />
    </bean>
    <!-- myBatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:/sqlMapper/*Mapper.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cainiaojava.dao.iface" />
        <!-- <property name="basePackage" value="druid-stat-interceptor" /> -->
        <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
    </bean>
</beans>

在dispatcher_cainiao.xml中引用一下这个配置文件:

 <!-- 加载其他两个配置文件 -->
      <import resource="classpath:spring-mybits.xml" />

数据库的配置信息

#mysql连接配置参数
url=jdbc:mysql://localhost:3306/cainiao
username=cainiao
password=cainiao
validationQuery=select 1 from dual

mybits的mapper文件:

<?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.cainiaojava.dao.iface.IUserManageDao">

    <!-- 查询所有用户-->
    <select id="getUsers" resultType="com.cainiaojava.beans.User">
        select * from user
    </select>
</mapper>

然后就是修改一下控制类:

@Controller
@RequestMapping("demo")
public class DispatcherController {

    @Autowired
    private IUserManage userManage;
    @RequestMapping(method=RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("list", userManage.getUsers());
//        System.out.println("index.jsp");
        return "index";
    }
}

然后就是后台的逻辑跳转了。

首先,在控制类里注册了用户管理接口IUserManage:

/**
 *  IUserManage : 用户管理接口
 * @author xuejupo  jpxue@travelsky.com
 * create in 2016-3-7 7:04:02
 */
public interface IUserManage {
    List<User> getUsers();
}

通过spring的注解,实现bean交给spring管理,注册的接口可以找到实现bean:UserManager:

/**
 *  UserManage : 用户管理具体实现类,负责具体的代码逻辑,调用dao层执行sql语句
 * @author xuejupo  jpxue@travelsky.com
 * create in 2016-3-7 7:04:43
 */

@Service("userManage")
public class UserManage implements IUserManage{
    @Autowired
    private IUserManageDao userManageDao;

    @Override
    public List<User> getUsers() {
        // TODO Auto-generated method stub
        return userManageDao.getUsers();
    }

}

然后,在用户管理类的实现类里,注册了dao文件IUserManageDao:

/**
 *  UserManageDao : mybits中的mapper文件实现sql的细节,此接口不用实现(dao只负责执行sql)
 * @author xuejupo  jpxue@travelsky.com
 * create in 2016-3-7 7:13:14
 */
@Component("userManageDao")
public interface IUserManageDao {
    /**
    * getUsers: 获取所有的用户,获取用户接口
    * @return
    * List<User>
     */
    public List<User> getUsers();
}

在dao层接口中,接口的方法是不用实现的,dao类只负责执行sql(根据mapper文件中的namespace找到接口,然后找跟方法名对应的id执行)。

执行效果就是这样:

PS:   附件是我的整个工程的压缩文件(里面还有redis,不过暂时没用到)。

如果需要下载我的工程然后执行的话,需要修改的地方就是需要自己新建一个数据库表(mysql)user,然后里面4个字段userId,userName,passwd,info。当然,也可以修改一下bean文件。

需要修改一下数据库配置文件jdbc.properties。

以后更新得可能会慢很多了。it这行,最好的学习方法永远是多用。  所以我想从零开始,利用spring和springMVC和mybits(可能会用到redis)造一个网站出来(初步打算是论坛)。不过因为公司也有工作,而且我的前端基础薄弱。。 所以进度可能无法掌握。。。 我会在iteye和我的博客园实时更新网站的进度,也会把我遇到的问题和解决方法更新到这里。

博客园这里上传附件好不方便。。。   感兴趣的朋友可以在这里下载我的附件http://709002341.iteye.com/admin/blogs/2283664

spring小例子-springMVC+mybits整合的小例子的更多相关文章

  1. 【SpringMVC学习04】Spring、MyBatis和SpringMVC的整合

    前两篇springmvc的文章中都没有和mybatis整合,都是使用静态数据来模拟的,但是springmvc开发不可能不整合mybatis,另外mybatis和spring的整合我之前学习mybati ...

  2. 《经久不衰的Spring框架:Spring+SpringMVC+MyBatis 整合》

    前言 主角即Spring.SpringMVC.MyBatis,即所谓的SSM框架,大家应该也都有所了解,概念性的东西就不写了,有万能的百度.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目 ...

  3. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  4. Spark小课堂Week7 从Spark中一个例子看面向对象设计

    Spark小课堂Week7 从Spark中一个例子看面向对象设计 今天我们讨论了个问题,来设计一个Spark中的常用功能. 功能描述:数据源是一切处理的源头,这次要实现下加载数据源的方法load() ...

  5. Spring Boot中的微信支付(小程序)

    前言 微信支付是企业级项目中经常使用到的功能,作为后端开发人员,完整地掌握该技术是十分有必要的. logo 一.申请流程和步骤 图1-1 注册微信支付账号 获取微信小程序APPID 获取微信商家的商户 ...

  6. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  7. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  8. 框架篇:Spring+SpringMVC+hibernate整合开发

    前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...

  9. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

随机推荐

  1. Firefox插件一键切换兼容IE

    转载:http://mozilla.com.cn/thread-42137-1-1.html 让火狐兼容IE的双核扩展,一键切换至IE内核,网银支付无忧愁.支持Adblock plus和FireGes ...

  2. 测试HAPROXY的文件分流办法

    测试HAPROXY的文件分流办法 http://blog.chinaunix.net/uid-20553497-id-3054980.html http://blog.sina.com.cn/s/bl ...

  3. Windows创建自动化任务

    Windows创建自动化任务使得开机就打开相应的Python目录 1:计算机管理 2:找到任务计划程序 3:创建基本任务 4:任务触发器 5: 建立bat执行文件 start "" ...

  4. 设备\Device\Harddisk1\DR1 有一个不对的区块

    近期遇到一个windows上的Oracle DB system表空间有问题.然后第一个反应就是查看windows的日志查看器,确实发现了报错: 设备\Device\Harddisk1\DR1 有一个不 ...

  5. [MongoDB] Insert, find -- 1

    MongoDB is JSON Document: How to start MongoDB client: mongod //start the server mongo // start the ...

  6. 强大的ASP.NET控件---验证控件

        学习完了牛腩之后,在进行ASP.NET的学习的时候,对全部学的知识.都有一种似曾相识的感觉,"哦,这个,在牛腩新闻公布系统中用过".仅仅只是那时候.用的也是迷迷糊糊的,就说 ...

  7. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  8. 和iPhone有关的视图控制器:UIViewController、UITabBarController、UINavigationController及其混合用法

    iPhone中的view视图是应用程序对于数据最直观.最直接的呈现方式,如下是我在学习了iPhone中的视图控制器以及由其衍生的特殊子类的总结,希望对那些初学者有所帮助: UIViewControll ...

  9. Xcode快照——管理应用程序版本

    转自:http://blog.csdn.net/yuanbohx/article/details/8919474 1.创建快照:FIle → Create Snapshot 2.查看快照:Window ...

  10. Android实现定时器的方法

    一.Handler 和 Thread package com.lstech.app; import android.app.Activity; import android.os.Bundle; im ...