01.MyBatis快速入门
1.下载jar包
Mybatis包+数据库驱动包
https://github.com/mybatis/mybatis-3/releases
2.新建Java工程并导入jar包

3.创建数据库与表
CREATE DATABSE mybatis01;
USE mybatis01;
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` date DEFAULT NULL COMMENT '生日',
`sex` char(1) DEFAULT NULL COMMENT '性别',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', '王五', null, '2', null);
INSERT INTO `user` VALUES ('10', '张三', '2014-07-10', '1', '北京市');
INSERT INTO `user` VALUES ('16', '张小明', null, '1', '河南郑州');
INSERT INTO `user` VALUES ('22', '陈小明', null, '1', '河南郑州');
INSERT INTO `user` VALUES ('24', '张三丰', null, '1', '河南郑州');
INSERT INTO `user` VALUES ('25', '陈小明', null, '1', '河南郑州');
INSERT INTO `user` VALUES ('26', '王五', null, null, null);
4.新建Mybatis核心配置文件+log4j配置文件
<?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>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis01?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="toor" />
</dataSource>
</environment>
</environments>
</configuration>
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
5.创建实体类和映射文件
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
<?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"> <!-- namespace:命名空间,用于隔离sql,还有一个很重要的作用,后面会讲 --> <mapper namespace="test"> </mapper>
6.在SqlMalConfig.xml中配置映射文件
<!-- 添加映射 -->
<mappers>
<mapper resource="cn/itcast/mybatis/po/User.xml"/>
</mappers>
7.测试
1.查询一条结果
<!-- 通过ID查询一个结果 -->
<select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">
select * from user where id = #{id}
</select>
/**
* 通过ID查询一个结果
* @throws Exception
*/
@Test
public void m01() throws Exception{
//加载配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行SQL
User user = sqlSession.selectOne("test.findUserById", 10);
System.out.println(user);
}

2.通过用户名模糊查询
<!-- 通过用户名模糊查询 -->
<select id="findUserByUsername" parameterType="String" resultType="cn.itcast.mybatis.po.User">
select * from user where username like "%"#{username}"%" <!-- SELECT * FROM `user` WHERE username LIKE '%${value}%' -->
</select>
//执行SQL
List<User> list = sqlSession.selectList("test.findUserByUsername", "王五");
for (User user : list) {
System.out.println(user);
}

3.添加用户
<!-- 添加用户 -->
<insert id="insert" parameterType="cn.itcast.mybatis.po.User">
insert into user(username,sex,birthday,address)
values(#{username},#{sex},#{birthday},#{address})
</insert>
//执行SQL
User user = new User();
user.setUsername("添加User");
user.setSex("男");
user.setBirthday(new Date());
user.setAddress("未知区域");
sqlSession.insert("test.insert", user);
//手动提交事务!!
sqlSession.commit();

4.添加用户,返回ID
<!-- 添加用户 返回ID -->
<insert id="insertRE" parameterType="cn.itcast.mybatis.po.User">
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,sex,birthday,address)
values(#{username},#{sex},#{birthday},#{address})
</insert>
//执行SQL
User user = new User();
user.setUsername("添加User");
user.setSex("男");
user.setBirthday(new Date());
user.setAddress("未知区域");
int id = sqlSession.insert("test.insertRE", user);
//手动提交事务!!
sqlSession.commit();
System.out.println(user.getId());

5.更新用户
<!-- 更新用户 -->
<update id="update" parameterType="cn.itcast.mybatis.po.User">
update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id = #{id}
</update>
//执行SQL
User user = new User();
user.setId(31);
user.setUsername("更新User");
user.setSex("女");
user.setBirthday(new Date());
user.setAddress("火星");
sqlSession.update("test.update", user);
//手动提交事务!!
sqlSession.commit();

6删除用户
<!-- 删除用户 -->
<delete id="delete" parameterType="int" >
delete from user where id=#{id}
</delete>
//执行SQL
sqlSession.delete("test.delete", 31);
//手动提交事务!!
sqlSession.commit();

01.MyBatis快速入门的更多相关文章
- MyBatis学习总结(一)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- MyBatis学习总结(一)——MyBatis快速入门(转载)
本文转载自http://www.cnblogs.com/jpf-java/p/6013537.html MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了 ...
- MyBatis入门学习教程-MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- MyBatis学习总结(一)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- 【转】MyBatis学习总结(一)——MyBatis快速入门
[转]MyBatis学习总结(一)——MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC ...
- MyBatis学习总结-MyBatis快速入门的系列教程
MyBatis学习总结-MyBatis快速入门的系列教程 [MyBatis]MyBatis 使用教程 [MyBatis]MyBatis XML配置 [MyBatis]MyBatis XML映射文件 [ ...
- MyBatis学习笔记(一)——MyBatis快速入门
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优 ...
- Java基础-SSM之mybatis快速入门篇
Java基础-SSM之mybatis快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 其实你可能会问什么是SSM,简单的说就是spring mvc + Spring + m ...
随机推荐
- How to SSH Into Your iPhone
First, I will explain what SSH is and why we do it. SSH (Secure Shell) allows you to exchange data b ...
- class9_Menubar 菜单
最终的运行效果图(程序见序号5) #!/usr/bin/env python# -*- coding:utf-8 -*-# -------------------------------------- ...
- WebStorm+Node.js开发环境的配置
1 下载地址: webstorm:http://www.jetbrains.com/webstorm node.js:https://nodejs.org/download/ 2 安装node.js ...
- A1016 Phone Bills (25 分)
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- 转:手机端html5触屏事件(touch事件)
touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...
- ps制作浮雕和投影效果
1用文字横排工具写个hope,按住ctrl+t可以调试出文字工具,上面直接用500点来改变文字的大小.2 用矩形选框工具直接可以切割图片的大小,然后双击一个图层,添加样式为浮雕....然后合并图层3 ...
- Vue——组件上使用v-model
一.最近在工作过程中要实现一个搜索模糊匹配功能,考虑到组件的复用,就单独把搜索框抽出来作为一个子组件.在以往的开发中,我一般会在input框中的值变化时向父组件emit一个事件,并带上一些父组件中需要 ...
- Hadoop的局限与不足
- shell 命令 查找命令find,grep
1.find 查找文件 [ find -name 文件名 ] 在当前目录及子目录中找这个文件 [ find -iname 文件名 ] 在当前目录及子目录中找这个文件,不区分大小写 [ find -na ...
- vue+ElementUI——表格分页(前端实现方法)
1.使用ElementUI中的<el-table></el-table>和 <el-pagination></el-pagination>组件来实现 2 ...