spring 新建mybatis ...
一. 创建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 ...的更多相关文章
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- IDEA中maven搭建Spring+SpringMVC+mybatis项目
一.介绍 使用IDEA搭建maven web项目,整合框架Spring+SpringMVC+mybatis 项目结构图:
- 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设置 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git小结(转)
摘要 出于兴趣,想要搭建一个自己的小站点,目前正在积极的准备环境,利用Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git,这里总结下最近遇到的一些问题及解决 ...
- [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World
来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...
- Spring整合MyBatis
前言:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的XML或注解用 ...
- Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成
与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
随机推荐
- 【vlfeat】O(n)排序算法——计数排序
今天想在网上找一个实现好的er算法来着,没啥具体的资料,无奈只能看vlfeat的mser源码,看能不能修修补补实现个er. 于是,看到某一段感觉很神奇,于是放下写代码,跑来写博客,也就是这段 /* - ...
- 20.ReenterLock重入锁
import java.util.concurrent.locks.ReentrantLock; /** * 重入锁 ReenterLock 一个线程允许连续获得同一把锁,注意:必须释放相同次数,释放 ...
- Session 工作原理
Session 工作原理 1.创建Session 当用户访问到一个服务器,如果服务器启用Session,服务器就要为该用户创建一个SESSION,在创建这个SESSION的时候,服务器首先检查这个用户 ...
- CDN技术之--流媒体CDN系统的组成
流媒体业务是一种对实时性.连续性.时序性要求非常高的业务,无论从带宽消耗上还是质量保障上来说,对best-effort的IP网络都是一个不小的冲击 –高带宽要求–高QoS要求–组播.广播要求(目前IP ...
- PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题
PHP CURL与file_get_contents函数都可以获取远程服务器上的文件保存到本地,但在性能上面两者完全不在同一个级别,下面我先来介绍PHP CURL或file_get_contents函 ...
- Linux系统Centos查看IP地址,不显示IP地址或者显示127.0.0.1
1.桌面界面 右上角有个电脑的图标,鼠标悬停会显示no network connect 点击一下图标,选择连接的网络则ok 2.命令行界面 在命令行界面输入 vi /etc/sysconfig/ne ...
- CentOS 7 & php7.2安装 php-redis 扩展
CentOS 7 & php7.2安装 php-redis 扩展 1.下载phpredis-developcd /tmpwget https://codeload.github.com/php ...
- linux安装相关软件
XShell上传jdk文件到Linux并安装配置1.yum -y install lrzsz2.sudo rz选文件3.sudo tar -zxvf jdk-8u131-linux-x64.tar.g ...
- jsp中jstl、el使用
tomcat7.0+JSTL1.1.2(不冲突) EL表达式获取变量 ${表达式} 如:${user.name} 不可以动态取值 ${user[name]}可以动态取值,变量名中含有特殊字符时只能用此 ...
- Spring事物的传播
spring的事物对于同一个类内部调用是不会生效的. 比如一个ServiceA,里面有个方法x()和y().其中x没有配置事物,而y配置的有实物.如果是一个没有事物的ServiceB调用了Servic ...