一. 创建bean类

package com.feilong.blog.dao;

public class Message {
private int id;
private String author;
private String content;
private String date;
public Message() {
// TODO Auto-generated constructor stub
}
public Message(String author, String content, String date) {
super();
this.author = author;
this.content = content;
this.date = date;
}
get and set method not show
}

二. 创建接口

package com.feilong.blog.dao;

import java.util.List;

public interface MessageDao {
public int insert(Message message);
// number 为 得到的Message的数量
public List<Message> getMessage(int number);
}

三. 创建Mapper.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.feilong.blog.dao.MessageDao">
<insert id="insert" parameterType="Message" >
INSERT message(author,content,date) VALUES(#{author},#{content},#{date})
</insert>
<select id="getMessage" parameterType="int" resultType="Message">
SELECT * FROM message ORDER BY id DESC LIMIT #{number}
</select>
</mapper>

四. 配置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>
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties" />
<typeAliases>
<package name="com.feilong.blog.dao" />
</typeAliases>
<environments default="mysql_development">
<environment id="mysql_development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/feilong/blog/dao/BlogMapper.xml" />
<mapper resource="com/feilong/blog/dao/MessageMapper.xml" />
</mappers>
</configuration>

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

<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties"/> <context:component-scan
base-package="com.feilong.blog.controller" />
<context:component-scan base-package="com.feilong.blog.dao">
</context:component-scan>
<!-- ViewResolver 视图解析器 --> <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 和 后缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.feilong.blog.Blog" />
<property name="configLocation" value="classpath:mybatis-config.xml "/>
</bean>
<bean id="blogdao" class="org.mybatis.spring.mapper.MapperFactoryBean ">
<property name="mapperInterface" value="com.feilong.blog.dao.BlogDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="messageDao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
<property name="mapperInterface" value="com.feilong.blog.dao.MessageDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>

六. 控制器

package com.feilong.blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.feilong.blog.dao.*;
@Controller
@RequestMapping
public class BlogController { @Autowired
private BlogDao blogDao = null ;
@Autowired
private MessageDao messageDao = null; private String author = "江期玉";
@RequestMapping(value= {"/","index"})
public String blogRequest(Model model) {
List<Blog> blogs = blogDao.getAll();
List<Message> messages = messageDao.getMessage(10);
model.addAttribute("blogs",blogs);
model.addAttribute("messages",messages);
model.addAttribute("author",author);
return "index";
} }

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. JS匿名包装器(自执行匿名函数)

    一.获得循环序号 for(var i = 0; i < 10; i++) { (function(e) { setTimeout(function() { console.log(e); }, ...

  2. 属性.native用于解决第三方el组件库@click事件无效

    描述 有时发现用一些第三方的组件库时,例如一个封装好的button按钮<el-butten>,绑定点击事件却没有任何作用,这时便需要加 .native 原因: v-on 是对 Vue 的事 ...

  3. 学习selenium grid记录

    1.找两台Windows系统,一个是A,作为Hub:一个是B,作为Node: 2.在A.B两台电脑分别下载selenium-server-standalone-2.48.0.jar,并放到指定目录 3 ...

  4. Java中volatile如何保证long和double的原子性操作

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426473.html 关键字volatile的主要作用是使变量在多个线程间可见,但无法保证原子性,对 ...

  5. 开放融合 | “引擎级”深度对接!POLARDB与SuperMap联合构建首个云原生时空平台

    阿里巴巴新一代自研云数据库POLARDB与超图软件SuperMap GIS实现 “引擎级”深度对接,构建了自治.弹性.高可用的云原生时空数据管理平台联合解决方案,推出了业界首个“云原生数据库+云原生G ...

  6. CSS 布局 - Overflow

    CSS 布局 - Overflow CSS overflow 属性用于控制内容溢出元素框时显示的方式. 这里的文本内容是可以滚动的,滚动条方向是垂直方向.dd马达价格 这里的文本内容是可以滚动的,滚动 ...

  7. hdu 3572 Task Schedule (Dinic模板)

    Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...

  8. uni-app获取元素宽高封装

    getElSize(id) { //得到元素的size return new Promise((res, rej) => { uni.createSelectorQuery().select(' ...

  9. 如何编写高性能的 javascript

    一.Javascript代码执行效率1. DOM1.1 使用 DocumentFragment 优化多次 append说明:添加多个 dom 元素时,先将元素 append 到 DocumentFra ...

  10. 在Linux终端命令行下播放音乐的命令

    现在的 Linux 桌面已经发展的很好了,在桌面下播放音乐操作起来也很简单.那么我们还记得在桌面不是那么好的时候我们是怎么播放音乐的么?哎,我是想不起来了,实在是太难了. 不过现在我们可以先安装一个小 ...