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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <!-- 加载映射文件 -->
<mappers>
<mapper resource="EmployeeMapper1.xml"/>
<mapper resource="EmployeeMapper2.xml"/>
</mappers>
</configuration>
 <?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"> <!-- demo1 -->
<mapper namespace="org.mybatis.example.EmployMapper">
<select id="selectEmp" resultType="org.entity.Employee">
<!-- 1、命名空间
2、唯一id
3、返回值类型
4、sql中的参数
#{id} 指从传递过来的参数中取出id值 相当于jdbc中的 "?"
select * from employee where id = #{id}
-->
select * from employee where id = #{id}
</select>
</mapper>
 package demo1;

 import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import entity.Employee; /**
* 1、创建javaBean 属性名需要跟数据表属性名相对应 如果不同可以再编写sql语句时使用别名 进行匹配
* 2、创建全局配置文件 mybatis-config.xml
* 3、创建SQL映射文件
* 4、将SQL映射文件加载进全局配置文件
* 5、编写代码
* 1)根据xml配置文件(全局配置文件)创建sessionFactory
* 2)sqlSessionFactory.openSession()获取 session
* 3)根据唯一标识执行映射的sql语句,并将结果封装进javaBean
* 4)关闭session
* @author Administrator
*
*/ public class Demo1 {
public static void main(String[] args) throws IOException {
/**
* 1、根据xml配置文件(全局配置文件)创建sessionFactory
*/
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
/**
* 2、获取 session
*/
SqlSession session = sqlSessionFactory.openSession();
/**
* 3、执行映射的sql语句,并将结果封装进javaBean
        session.selectList(arg0);
session.select(arg0, arg1);
     session.selectMap(arg0, arg1);
     session.selectOne(arg0);
      
session中本身有一些及基础的操作可以结合 mapper.xml 使用
*/   
//第一个参数为SQL语句映射,为避免命名冲突可以使用命名空间加id标识
// 第二个参数为sql语句中传入的参数
Employee employee = session.selectOne("org.mybatis.example.EmployMapper.selectEmp",1);
System.out.println(employee.toString());
session.close();
}
}

最新文章

  1. JavaScript闭包浅谈
  2. 遭遇input与button按钮背景图失效不显示的解决办法
  3. php中的循环遍历 foreach list each
  4. cache buffers
  5. ZK 使用Clients.response
  6. linux iftop流量查看工具的安装与使用
  7. 在aspx怎么引用public string getPicurl(string picurl)?
  8. SharePoint 2010 ——自定义上传页面与多文件上传解决方案
  9. EXT学习之——Extjs 文本框 TextField 添加点击(onclick)事件方法
  10. 使用C语言在Win控制台中实现指定位置输出
  11. HDU4871 Shortest-path tree(树分治)
  12. HDU 4599 Dice (概率DP+数学+快速幂)
  13. Android学习系列(23)--App主界面实现
  14. Asp.net 菜单控件
  15. javaWeb RSA加密使用
  16. DevExpress ASP.NET 使用经验谈(8)-ASPxGridView自定义列和基本事件
  17. Nginx+phpfastcgi下flush输出问题
  18. maven自定义骨架
  19. bzoj2730(矿场搭建)
  20. SQLAlchemy增删改查

热门文章

  1. linux服务器性能检测工具nmon使用
  2. ubuntu+let&#39;s encrypt生成永久免费https证书 ubuntu+tomcat+nginx+let&#39;s encrypt
  3. HBase数据同步到ElasticSearch的方案
  4. Nginx:subrequest的使用方式
  5. 把质量控制工作往前推进(1)——安装sonarqube
  6. DevOpsDays 活动咨询网站
  7. Android应用程序开发以及背后的设计思想深度剖析
  8. python中wxpython用法
  9. centos7 firefox 安装flash
  10. oracle分区表和分区索引概述