最基础的mybatis入门demo
demo结构

数据库情况 (不会转sql语句 骚瑞)


数据库连接信息 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mysql_demo jdbc.username=root jdbc.password=root
javabean Student.class
package entity;
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
private Integer tId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer gettId() {
return tId;
}
public void settId(Integer tId) {
this.tId = tId;
}
}
mybatis配置 mybatis-cfg.xml
<?xml version="1.0" encoding="UTF-8" ?><!--xml版本声明-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--xml约束-->
<configuration> <!--Configuration配置;结构;外形-->
<properties resource="resource/jdbc.properties"/> <!--加载配置文件->jdbc.properties 数据库文件-->
<typeAliases><!--对象映射的位置-->
<!--<typeAlias type="entity.Student"/>直接写n个对象-->
<!--<package name="entity"/> 扫描包-->
<package name="entity"/>
</typeAliases>
<environments default="mybatis-demo"><!--id随便写-->
<environment id="mybatis-demo"><!--随便写-->
<transactionManager type="JDBC"></transactionManager><!--事务管理器 目前是JDBC 以后交给Spring管理事务-->
<dataSource type="POOLED"><!--数据源 POOLED相当于连接池 池里放链接 -->
<property name="driver" value="${jdbc.driver}"/><!--${}用于读取上面jdbc.properties配置文件-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/><!--以上是通过反射原理注入-->
</dataSource>
</environment>
</environments>
<mappers><!--声明配置的mapper映射位置 可写文件 这里写的是包-->
<package name="dao"/>
</mappers>
</configuration>
定义查询接口StudentMapper.java
import java.util.List;
/**
* Created by zekai on 2017/6/10.
*/
public interface StudentMapper {//该接口只定义查询方法 不执行具体查询
//接口方法默认自带public
int insertStudent(Student student) throws Exception;// 插入 int判断是否执行成功 成功返回1
Student selectOneById(int id) throws Exception;//查询一条数据
List<Student> selectAllStudent();//查询列表 封装到list中
}
映射文件 StudentMapper.xml (必须与StudentMapper同名)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentMapper"><!--标志mapper(映射)是哪一个接口的mapper-->
<resultMap id="studentMapper" type="entity.Student"><!--声明mapper的id type表示映射道德javabean-->
<id column="Sid" property="id"/><!--id 主键 column☞数据库 property☞javabean 这里省略了java type等-->
<result property="name" column="Sname"/>
<result property="sex" column="Ssex"/>
<result property="tId" column="Tid"/>
<result property="age" column="SageNum"/>
</resultMap>
<insert id="insertStudent" parameterType="entity.Student" ><!--parameter表示传入参数是啥--><!--对应StudentMapper接口里的方法名-->
INSERT INTO student (Sid,Sname,Ssex,Tid,SageNum)VALUES (#{id},#{name},#{sex},#{tId},#{age})
<!--#{}对应studentmapper里的参数-->
</insert>
<select id="selectOneById" resultMap="studentMapper">
SELECT * FROM student WHERE Sid=#{id}
</select>
<select id="selectAllStudent" resultMap="studentMapper"><!--resultType可设定返回类型-->
SELECT *FROM student
</select>
</mapper>
测试方法(debug) Main.java
import dao.StudentMapper;
import entity.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
//根据配置文件生成sqlsessionfactory
/**
* Created by zekai on 2017/6/10.
*/
public class Main {
public static void main(String[] args) throws Exception {
//io加载配置文件
InputStream in=Main.class.getResourceAsStream("resource/mybatis-cfg.xml");
//用构建器构建一个inputstream
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
SqlSession session=factory.openSession();
//取得mapper对象 调用mapper方法
StudentMapper mapper=session.getMapper(StudentMapper.class);
Student student=new Student();
// student.setId(20);
// student.setAge(28);
// student.setName("alowang");
// student.settId(2);
// mapper.insertStudent(student);
// student=mapper.selectOneById(2);
List<Student> studentList=mapper.selectAllStudent();
//记得提交 不提交等于啥都没干
session.commit();
//关闭资源
session.close();
}
}
最基础的mybatis入门demo的更多相关文章
- Mybatis入门Demo(单表的增删改查)
1.Mybatis 什么是Mybatis: mybatis是一个持久层框架,用java编写的 它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动.创建连接等繁杂过程 ...
- MyBatis 入门Demo
新建数据库my_db,新建表student_tb id为主键,不自动递增. 不必插入数据. 下载MyBatis https://github.com/mybatis/mybatis-3/release ...
- Mybatis入门DEMO
下面将通过以下步骤说明如何使用MyBatis开发一个简单的DEMO: 步骤一:新建表STUDENTS 字段有: Stu_Id.Stu_Name.Stu_Age.Stu_Birthday CREATE ...
- MyBatis基础:MyBatis入门(1)
1. MyBatis简介 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyBatis ...
- Mybatis入门和简单Demo
一.Mybatis的诞生 回顾下传统的数据库开发,JDBC和Hibernate是使用最普遍的技术,但这两种ORM框架都存在一定的局限性: JDBC:最原生的技术,简单易学,执行速度快,效率高,适合大数 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- MyBatis入门基础(一)
一:对原生态JDBC问题的总结 新项目要使用mybatis作为持久层框架,由于本人之前一直使用的Hibernate,对mybatis的用法实在欠缺,最近几天计划把mybatis学习一哈,特将学习笔记记 ...
- MyBatis入门基础
转自http://www.cnblogs.com/selene/p/4604605.html 话不多说,先看看原始的JDBC程序代码,看看这样的代码存在什么样子的问题. package com.uti ...
- 【SSH系列】初识spring+入门demo
学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...
随机推荐
- My SQL数据库的安装与配置
MySQL是一个关系型数据库管理系统.MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言 MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小.速度快.总体拥有成本低,尤 ...
- 比较Java中几个常用集合添加元素的效率
初始化需要进行比较的集合,统一增加10万个元素,获取整个过程的执行时间. 1.List集合增加元素 private static void testList() { List<Integer&g ...
- codeforces 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
- Oracle GoldenGate中HANDLECOLLISIONS参数使用详解
Oracle GoldenGate中HANDLECOLLISIONS参数使用详解 HANDLECOLLISIONS 是一个 replicat 进程参数,主要在 initial load 中使用.在 ...
- single number i && ii && iii
Problem statement Elementary knowledge: There is a popular question when I seeked my job at Beijing: ...
- 【Android Widget】1.TextView
1.创建可被点击的TextView 1.1 在xml中创建可被点击的TextView android:autoLink 是否将符合指定格式的文本转换成可单击的超链接. 属性值可以是如下几个属性值的一个 ...
- 四、I/O
九.什么是I/O: 9.1.在Windows程序中,基础的运行单位为线程,为每一个线程分配一个处理器,可以让系统执行多个操作, 9.2.当线程进行一个I/O操作时,会被挂起,从而影响性能,为了解决这类 ...
- ZooKeeper 入门
0 介绍 官网:http://zookeeper.apache.org/ ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分 ...
- 思考一个关于Lambda表达式做为linq条件的问题
有一个集合如下 List<User> users = new List<User> { new User{Name = "1",Aget = 12}, ne ...
- Linux设备中的并发控制
一.自旋锁1.定义自旋锁:spinlock_t lock2.初始化自旋锁:spin_lock_init(lock)3.获得自旋锁:spin_lock(lock)4.释放自旋锁:spin_unlock( ...