一. 创建bean类

  1. package com.feilong.blog.dao;
  2.  
  3. public class Message {
  4. private int id;
  5. private String author;
  6. private String content;
  7. private String date;
  8. public Message() {
  9. // TODO Auto-generated constructor stub
  10. }
  11. public Message(String author, String content, String date) {
  12. super();
  13. this.author = author;
  14. this.content = content;
  15. this.date = date;
  16. }
  17. get and set method not show
  18. }

二. 创建接口

  1. package com.feilong.blog.dao;
  2.  
  3. import java.util.List;
  4.  
  5. public interface MessageDao {
  6. public int insert(Message message);
  7. // number 为 得到的Message的数量
  8. public List<Message> getMessage(int number);
  9. }

三. 创建Mapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.feilong.blog.dao.MessageDao">
  6. <insert id="insert" parameterType="Message" >
  7. INSERT message(author,content,date) VALUES(#{author},#{content},#{date})
  8. </insert>
  9. <select id="getMessage" parameterType="int" resultType="Message">
  10. SELECT * FROM message ORDER BY id DESC LIMIT #{number}
  11. </select>
  12. </mapper>

四. 配置mybatis-config.xml 映射

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5.  
  6. <configuration>
  7. <!-- 加载类路径下的属性文件 -->
  8. <properties resource="db.properties" />
  9. <typeAliases>
  10. <package name="com.feilong.blog.dao" />
  11. </typeAliases>
  12. <environments default="mysql_development">
  13. <environment id="mysql_development">
  14. <transactionManager type="JDBC" />
  15. <dataSource type="POOLED">
  16. <property name="driver" value="${mysql.driver}" />
  17. <property name="url" value="${mysql.url}" />
  18. <property name="username" value="${mysql.username}" />
  19. <property name="password" value="${mysql.password}" />
  20. </dataSource>
  21. </environment>
  22. </environments>
  23. <mappers>
  24. <mapper resource="com/feilong/blog/dao/BlogMapper.xml" />
  25. <mapper resource="com/feilong/blog/dao/MessageMapper.xml" />
  26. </mappers>
  27. </configuration>

五. 在 springmvc-config.xml 里配置bean message

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd">
  14.  
  15. <context:property-placeholder location="classpath:db.properties"/>
  16.  
  17. <context:component-scan
  18. base-package="com.feilong.blog.controller" />
  19. <context:component-scan base-package="com.feilong.blog.dao">
  20. </context:component-scan>
  21. <!-- ViewResolver 视图解析器 -->
  22.  
  23. <bean
  24. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  25. <!-- 前缀 和 后缀 -->
  26. <property name="prefix" value="/WEB-INF/jsp/" />
  27. <property name="suffix" value=".jsp" />
  28. </bean>
  29. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  30. <property name="driverClassName" value="${mysql.driver}" />
  31. <property name="url" value="${mysql.url}" />
  32. <property name="username" value="${mysql.username}" />
  33. <property name="password" value="${mysql.password}" />
  34. </bean>
  35.  
  36. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  37. <property name="dataSource" ref="dataSource"></property>
  38. <property name="typeAliasesPackage" value="com.feilong.blog.Blog" />
  39. <property name="configLocation" value="classpath:mybatis-config.xml "/>
  40. </bean>
  41. <bean id="blogdao" class="org.mybatis.spring.mapper.MapperFactoryBean ">
  42. <property name="mapperInterface" value="com.feilong.blog.dao.BlogDao" />
  43. <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  44. </bean>
  45. <bean id="messageDao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
  46. <property name="mapperInterface" value="com.feilong.blog.dao.MessageDao"/>
  47. <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  48. </bean>
  49. </beans>

六. 控制器

  1. package com.feilong.blog.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import com.feilong.blog.dao.*;
  10. @Controller
  11. @RequestMapping
  12. public class BlogController {
  13.  
  14. @Autowired
  15. private BlogDao blogDao = null ;
  16. @Autowired
  17. private MessageDao messageDao = null;
  18.  
  19. private String author = "江期玉";
  20. @RequestMapping(value= {"/","index"})
  21. public String blogRequest(Model model) {
  22. List<Blog> blogs = blogDao.getAll();
  23. List<Message> messages = messageDao.getMessage(10);
  24. model.addAttribute("blogs",blogs);
  25. model.addAttribute("messages",messages);
  26. model.addAttribute("author",author);
  27. return "index";
  28. }
  29.  
  30. }

spring 新建mybatis ...的更多相关文章

  1. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  2. IDEA中maven搭建Spring+SpringMVC+mybatis项目

    一.介绍 使用IDEA搭建maven web项目,整合框架Spring+SpringMVC+mybatis 项目结构图:

  3. SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    1.前言 使用框架都是较新的版本: Spring 4.0.2 RELEASE Spring MVC 4.0.2 RELEASE MyBatis 3.2.6 2.Maven引入需要的JAR包 2.1设置 ...

  4. Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)

    依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  5. Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git小结(转)

    摘要 出于兴趣,想要搭建一个自己的小站点,目前正在积极的准备环境,利用Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git,这里总结下最近遇到的一些问题及解决 ...

  6. [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World

    来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...

  7. Spring整合MyBatis

    前言:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的XML或注解用 ...

  8. Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  9. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

随机推荐

  1. kvm学习笔记(一,基础概念)

    前言 阿里云的云主机,采用的kvm,今天就花了点时间研究了下. 一.安装 官方文档参考:http://www.linux-kvm.org/page/HOWTO 二.快速建立一个基于vnc的虚拟机安装 ...

  2. 表结构转excel

    SELECTCOLUMN_NAME 列名,COLUMN_TYPE 数据类型,DATA_TYPE 字段类型,CHARACTER_MAXIMUM_LENGTH 长度,IS_NULLABLE 是否为空,CO ...

  3. translation of 《deep learning》 Chapter 1 Introduction

    原文: http://www.deeplearningbook.org/contents/intro.html Inventors have long dreamed of creating mach ...

  4. vs code常用插件(python)

    1.chinese 作用:vscode设置为中文. 使用方法:Ctrl+Shift+P:输入 "config":选择zh 2.python 作用:调试 3.autoDocstrin ...

  5. python字符串的截取,查找

    1.字符串的截取 str = "123456" str[:3] = 123 str[1:3] = 23 str[0:-1] = 12345 里面的数字都是index索引,从第一个索 ...

  6. restful规范面试总结

    1.url链接设计:采用https方式,有api关键字,有版本需要明确版本,请求链接用名词来表示资源,具体的操作方式采用请求方式来确定2.url响应数据设计:需要明确 状态码.错误信息.成功结果,子资 ...

  7. kubernetes批量删除pod

    监控页面出现看到有运行失败的pod 1) 查看有哪些不运行的podcustom-metrics-apiserver日志占满空间被驱逐 [root@hadoop03 ~]# kubectl get po ...

  8. 2018icpc南京/gym101981 A Adrien and Austin 博弈

    题意: n个连续排列的石子,每次只许拿连续的(中间没有空格)的k个,问你谁必胜 题解: 简单博弈,特判总数为0,k=1两种情况,其他情况先拿必胜,方法是拿掉中间的,然后对方怎么拿你镜面拿就行. #in ...

  9. 《ArcGIS Runtime SDK for .NET开发笔记》--三维功能

    介绍 在ArcGIS Runtim SDK for .NET 10.2.6中,新添加了三维地图功能.在ArcGIS中,我们将三维地图称为Scene(场景),所以在Runtime SDK SDK for ...

  10. python中的pow()函数解释

    转载自:https://blog.csdn.net/wuxiaobingandbob/article/details/47776209 Python中pow(),里面可以有两个或三个参数,它们的意义是 ...