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"; } }