标签:
主配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE configuration 4 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 5 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 6 7 <configuration> 8 9 <settings> 10 <setting name="useGeneratedKeys" value="false"/> 11 <setting name="useColumnLabel" value="true"/> 12 </settings> 13 14 <typeAliases> 15 <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/> 16 </typeAliases> - 17 <environments default="development"> 18 <environment id="development"> 19 <transactionManager type="JDBC"> 20 <property name="" value=""/> 21 </transactionManager> 22 <dataSource type="UNPOOLED"> 23 <property name="driver" value="com.mysql.jdbc.Driver"/> 24 <property name="url" value="jdbc:mysql://127.0.0.1:3306/micro_message"/> 25 <property name="username" value="root"/> 26 <property name="password" value="root"/> 27 </dataSource> 28 </environment> 29 </environments> 30 31 <mappers> 32 <mapper resource="com/imooc/config/sqlxml/Message.xml"/> 33 </mappers> 34 35 </configuration>
映射文件
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 6 <mapper namespace="org.zln.domain.Message"> 7 8 <resultMap id="messageResult" type="message"> 9 <id property="id" column="ID" jdbcType="INTEGER"/> 10 <result property="command" column="COMMAND" jdbcType="VARCHAR"/> 11 <result property="description" column="DESCRIPTION" jdbcType="VARCHAR"/> 12 <result property="content" column="CONTENT" jdbcType="VARCHAR"/> 13 </resultMap> 14 15 <select id="queryMessageList" resultMap="messageResult"> 16 SELECT id,command,description,content FROM message 17 </select> 18 </mapper>
初始化配置文件
1 package com.imooc.db; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 /** 12 * 访问数据库类 13 */ 14 public class DBAccess { 15 public SqlSession getSqlSession() throws IOException { 16 // 通过配置文件获取数据库连接信息 17 Reader reader = Resources.getResourceAsReader("com/imooc/config/Configuration.xml"); 18 // 通过配置信息构建一个SqlSessionFactory 19 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 20 // 通过sqlSessionFactory打开一个数据库会话 21 SqlSession sqlSession = sqlSessionFactory.openSession(); 22 return sqlSession; 23 } 24 }
Dao
1 package org.zln.dao; 2 3 import org.springframework.stereotype.Repository; 4 import org.zln.domain.Message; 5 6 import java.util.List; 7 8 /** 9 * Created by sherry on 000006/7/6 19:16. 10 */ 11 @Repository 12 public class MessageDao extends BaseDao { 13 14 public List<Message> queryMessageList(String command,String description){ 15 return sqlSessionTemplate.selectList("org.zln.domain.Message.queryMessageList"); 16 } 17 }
在MyBatis中,必须使用命名空间,不是在调用sql的时候,没有书写命名空间也能找到对应的id
标签:
原文地址:http://www.cnblogs.com/sherrykid/p/4628816.html