创建实体类:

package com.test.mybatis.bean;

public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + "]";
} }

配置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>
<!--
1、mybatis可以使用properties来引入外部配置文件的内容
resource:引入类路径下的资源
url:引入网络路径或者磁盘路径下的资源
-->
<properties resource="jdbcconf.properties">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</properties>
<!--
2、settings包含很多的设置项
setting:用来设置每一个设置项
name:设置项名
value:设置项取值
-->
<settings>
<!--
驼峰命名规则:当数据库名与实体类名发生不一致时,驼峰命名则会发挥作用
如:
数据库字段:id stuname
实体类属性:id stuName
-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--
3、typeAliases:别名处理器,可以为Java类型起别名
别名不区分大小写
-->
<typeAliases>
<!--
type:指定要起别名的类型全类名;默认别名就是类名小写:
alias:指定新的别名
-->
<!-- <typeAlias type="com.test.mybatis.Employee" alias="emp"/> -->
<!--
package:为某个包下的所有类批量起别名
也可在具体的实体类中使用@Alias注解为某个类型指定新的别名
-->
<package name="com.test.mybatis.bean"/>
</typeAliases>
<!-- 配置环境 -->
<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"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到 mybatis-config.xml中-->
<mappers>
<mapper resource="EmployeeMapper.xml"/>
</mappers> </configuration>

创建接口:

package com.test.mybatis.dao;

import com.test.mybatis.bean.Employee;

public interface EmployeeMapper {

    public Employee getEmpById(Integer id);
}

创建实现接口中方法的配置文件,通过namespace就行绑定

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mtbatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mybatis.dao.EmployeeMapper">
<!--
namespace:名称空间 指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中去吃id值
-->
<select id="getEmpById" resultType="employee">
select * from tbl_employee where id=#{id}
</select>
</mapper>

创建测试类:

package com.test.mybatis.test;

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 org.junit.Test; import com.test.mybatis.bean.Employee;
import com.test.mybatis.dao.EmployeeMapper; public class MyBatisTest {
//读取配置文件,得到SQLSessionFactory
public SqlSessionFactory getSqlSessionFactory() throws IOException{
String resource="mybatis-config.xml";
InputStream is=Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(is);
}
@Test
public void test() throws IOException{
//获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//获取sqlSession
//代表和数据库的一次对话,用完必须关闭,相当于connection,都是非线程安全。每次使用都应该获取新的对象
SqlSession openSession = sqlSessionFactory.openSession();
try{
//获取接口的实现类对象
//会为接口自动的创建一个代理对象,代理对象去执行增删查改方法
//mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象
//(将接口和xml进行绑定)
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpById(1);
System.out.println(emp);
}finally{
openSession.close();
}
} }

Mybatis实例及配置(一)的更多相关文章

  1. ibatis mybatis sql语句配置 符号不兼容 大于号 小于号<!CDATA[ ]>

    ibatis mybatis sql语句配置 符号不兼容 大于号 小于号<!CDATA[ ]> 因为这个是xml格式的,所以不允许出现类似">"这样的字符,但是都 ...

  2. Spring Boot 数据访问集成 MyBatis 与事物配置

    对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...

  3. Mybatis的核心配置

    之前了解了Mybatis的基本用法,现在学习一下Mybatis框架中的核心对象以及映射文件和配置文件,来深入的了解这个框架. 1.Mybatis的核心对象 使用MyBatis框架时,主要涉及两个核心对 ...

  4. MyBatis实例教程--开发环境搭建

    MyBatis实例教程--开发环境搭建 准备工作: 1.mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包 ...

  5. SpringBoot系列-整合Mybatis(XML配置方式)

    目录 一.什么是 MyBatis? 二.整合方式 三.实战 四.测试 本文介绍下SpringBoot整合Mybatis(XML配置方式)的过程. 一.什么是 MyBatis? MyBatis 是一款优 ...

  6. 《连载 | 物联网框架ServerSuperIO教程》2.服务实例的配置参数说明

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍  <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制 一.综述 SuperIO(SIO)定位 ...

  7. (转)springMVC+mybatis+ehcache详细配置

    一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...

  8. MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql

    一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...

  9. mysql多实例的配置和管理

    原文地址:mysql多实例的配置和管理 作者:飞鸿无痕 多实例mysql的安装和管理 mysql的多实例有两种方式可以实现,两种方式各有利弊.第一种是使用多个配置文件启动不同的进程来实现多实例,这种方 ...

随机推荐

  1. Java代码编写规范(转载)

    编码规范 1 前言为确保系统源程序可读性,从而增强系统可维护性,java编程人员应具有基本类似的编程风格,兹制定下述Java编程规范,以规范系统Java部分编程.系统继承的其它资源中的源程序也应按此规 ...

  2. Java web 小测验

    题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...

  3. GCN 入门

    参考链接: https://www.zhihu.com/question/54504471/answer/611222866 1 拉普拉斯矩阵 参考链接: http://bbs.cvmart.net/ ...

  4. python_appium使用原理

    一. appium介绍 Appium是一个开源测试自动化框架,可用于原生,混合和移动Web应用程序测试. 它使用WebDriver协议驱动iOS,Android和Windows应用程序. 多平台支持: ...

  5. Vue Element-UI 中列表单选的实现

    el-table中单选的实现 引用场景: 选择单条数据进行业务操作 实现方式: 给el-table-column设置el-radio Template 代码 <div class="r ...

  6. IPSec传输模式下的ESP报文的装包和拆包过程

    IPSec协议定义 IPsec将IP数据包的内容在装包过程在网络层先加密再传输,即便中途被截获,由于缺乏解密数据包所必要的密钥,攻击者也无法获取里面的内容. IPsec 对数据进行加密的方式 加密模式 ...

  7. Git的使用方法及IDEA与Git的集成

    一.Git的环境配置 1.Git软件下载 (下载地址:https://git-scm.com/)由于国外的网站下载的超慢可以使用国内的阿里的开源镜像下载(下载地址:https://npm.taobao ...

  8. 如何配置 SSH 密钥连接 Git 仓库

    SSH 是 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:是建立在应用层基础上的安全协议. SSH 是目前较可靠,专为远程登录会话和其 ...

  9. IE浏览器连接WebSocket报错:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    在项目开发中整合了WebSocket,本来没什么问题了,但是偶尔发现用IE浏览器打开web端不能推送消息,因为PC端与服务器建立连接失败了.网上查了很多资料, 又看了看源码,都不对症:又怀疑是Spri ...

  10. 第六篇Scrum冲刺博客--Interesting-Corps

    第六篇Scrum冲刺博客 站立式会议 1.会议照片 2.队友完成情况 团队成员 昨日完成 今日计划 鲍鱼铭 搜索页面以及音乐详情页面数据导入及测试 各界面数据请求云函数设计及实现 叶学涛 进行页面的优 ...