创建数据库
-- ---------------------------- -- Table structure for user -- ----------------------------
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;
-- ---------------------------- -- Records of user -- ----------------------------
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', '河南郑州');
Mybatis框架原理(掌握)
1、Mybatis 是什么?
Mybatis 是一个持久层的架构,是 apache 下的顶级项目。
Mybatis 原先是托管在 googlecode 下,再后来是托管在 Github 上。
Mybatis 让程序员将主要的精力放在 sql 上,通过 Mybatis 提供的映射方式,自由灵活生成(半自动,大部分需要程序员编写 sql )满足需要 sql 语句。
Mybatis 可以将向 preparedStatement 中的输入参数自动进行输入映射,将查询结果集灵活的映射成 java 对象。(输出映射)
1、Mybatis 是什么?
Mybatis 是一个持久层的架构,是 apache 下的顶级项目。
Mybatis 原先是托管在 googlecode 下,再后来是托管在 Github 上。
Mybatis 让程序员将主要的精力放在 sql 上,通过 Mybatis 提供的映射方式,自由灵活生成(半自动,大部分需要程序员编写 sql )满足需要 sql 语句。
Mybatis 可以将向 preparedStatement 中的输入参数自动进行输入映射,将查询结果集灵活的映射成 java 对象。(输出映射)
•SqlMapConfig.xml (Mybatis的全局配置文件,名称不定)配置了数据源、事务等 Mybatis 运行环境
•Mapper.xml 映射文件(配置 sql 语句)
•SqlSessionFactory (会话工厂)根据配置文件配置工厂、创建 SqlSession
•SqlSession (会话)面向用户的接口、操作数据库(发出 sql 增删改查)
•Executor (执行器)是一个接口(基本执行器、缓存执行器)、SqlSession 内部通过执行器操作数据库
•Mapped Statement (底层封装对象)对操作数据库存储封装,包括 sql 语句、输入参数、输出结果类型
Mybatis入门程序
实现以下功能:
•根据用户id查询一个用户信息
•根据用户名称模糊查询用户信息列表
•添加用户
•更新用户
•删除用户
2、环境
java 环境 :jdk1.8.0_77
开发工具 : IDEA 2016.1
数据库 : MySQL 5.7
Mybatis 运行环境( jar 包)
MySQL 驱动包
其他依赖包
3、 log4j.properties
在classpath下创建log4j.properties如下:
Global logging configuration
#在开发环境日志级别要设置为DEBUG、生产环境要设置为INFO或者ERROR 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
EmpMapper.xml
<?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" >
<mapper namespace="com.mapper.EmpMapper" >
<resultMap id="BaseResultMap" type="com.company.Emp" >
<id column="empno" property="empno" jdbcType="INTEGER" />
<result column="ename" property="ename" jdbcType="VARCHAR" />
<result column="job" property="job" jdbcType="VARCHAR" />
<result column="mgr" property="mgr" jdbcType="INTEGER" />
<result column="hiredate" property="hiredate" jdbcType="DATE" />
<result column="sal" property="sal" jdbcType="DECIMAL" />
<result column="comm" property="comm" jdbcType="DECIMAL" />
<result column="deptno" property="deptno" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
empno, ename, job, mgr, hiredate, sal, comm, deptno
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from emp
where empno = #{empno,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from emp
where empno = #{empno,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.company.Emp" >
insert into emp (empno, ename, job,
mgr, hiredate, sal, comm,
deptno)
values (#{empno,jdbcType=INTEGER}, #{ename,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR},
#{mgr,jdbcType=INTEGER}, #{hiredate,jdbcType=DATE}, #{sal,jdbcType=DECIMAL}, #{comm,jdbcType=DECIMAL},
#{deptno,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.company.Emp" >
insert into emp
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="empno != null" >
empno,
</if>
<if test="ename != null" >
ename,
</if>
<if test="job != null" >
job,
</if>
<if test="mgr != null" >
mgr,
</if>
<if test="hiredate != null" >
hiredate,
</if>
<if test="sal != null" >
sal,
</if>
<if test="comm != null" >
comm,
</if>
<if test="deptno != null" >
deptno,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="empno != null" >
#{empno,jdbcType=INTEGER},
</if>
<if test="ename != null" >
#{ename,jdbcType=VARCHAR},
</if>
<if test="job != null" >
#{job,jdbcType=VARCHAR},
</if>
<if test="mgr != null" >
#{mgr,jdbcType=INTEGER},
</if>
<if test="hiredate != null" >
#{hiredate,jdbcType=DATE},
</if>
<if test="sal != null" >
#{sal,jdbcType=DECIMAL},
</if>
<if test="comm != null" >
#{comm,jdbcType=DECIMAL},
</if>
<if test="deptno != null" >
#{deptno,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.company.Emp" >
update emp
<set >
<if test="ename != null" >
ename = #{ename,jdbcType=VARCHAR},
</if>
<if test="job != null" >
job = #{job,jdbcType=VARCHAR},
</if>
<if test="mgr != null" >
mgr = #{mgr,jdbcType=INTEGER},
</if>
<if test="hiredate != null" >
hiredate = #{hiredate,jdbcType=DATE},
</if>
<if test="sal != null" >
sal = #{sal,jdbcType=DECIMAL},
</if>
<if test="comm != null" >
comm = #{comm,jdbcType=DECIMAL},
</if>
<if test="deptno != null" >
deptno = #{deptno,jdbcType=INTEGER},
</if>
</set>
where empno = #{empno,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.company.Emp" >
update emp
set ename = #{ename,jdbcType=VARCHAR},
job = #{job,jdbcType=VARCHAR},
mgr = #{mgr,jdbcType=INTEGER},
hiredate = #{hiredate,jdbcType=DATE},
sal = #{sal,jdbcType=DECIMAL},
comm = #{comm,jdbcType=DECIMAL},
deptno = #{deptno,jdbcType=INTEGER}
where empno = #{empno,jdbcType=INTEGER}
</update>
</mapper>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///java16 jdbc.username=root
jdbc.password=root
SqlMapConfig.xml
配置 Mybatis 的运行环境、数据源、事务等
<?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>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.mapper"></package>
</mappers>
</configuration>
Mybatis 的 mapper 接口
思路
程序员需要编写mapper接口(相当于Dao接口,增删改查操作)
程序员需要编写 mapper.xml 映射文件,需遵循一些开发规范,mybatis 可以自动生成 mapper 接口类代理对象。
开发规范:
1.在 mapper.xml 中 namespace 等于 mapper 接口地址(所在包名的全路径)
2.在 xxxmapper.java 接口中的方法名要与 xxxMapper.xml 中 statement 的 id 一致。
3.在 xxxmapper.java 接口中的输入参数类型要与 xxxMapper.xml 中 statement 的 parameterType 指定的参数类型一致。
4.在 xxxmapper.java 接口中的返回值类型要与 xxxMapper.xml 中 statement 的 resultType 指定的类型一致。
5.接口文件名要与xml映射文件名一致(UserMapper.java和UserMapper.xml)
7、根据用户 id(主键)查询用户信息
定义Mapper接口
public interface UserMapper {
public User findUserById(int id);
}
定义UserMapper.xml
<?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">
<mapper namespace="com.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="com.domain.User"> select * from user where id = #{value}
</select>
</mapper>
主方法测试
public static void main(String[] args) throws
IOException { //创建sqlSessionFactory //Mybatis
配置文件 String resource = "SqlMapConfig.xml";
// 得到配置文件流 InputStream inputStream =
Resources.getResourceAsStream(resource);
//创建会话工厂,传入Mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession =
sqlSessionFactory.openSession(); //创建
usermapper对象,mybatis自动生成代理对象
UserMapper userMapper =
sqlSession.getMapper(UserMapper.class); //调用
UserMapper的方法 User user =
userMapper.findUserById(1);
System.out.println(user.getUsername()); }
查找全部用户
public interface UserMapper {
public User findUserById(int id);
public List<User> findAllUsers();
//添加用户信息 public int
addUser(User user); //删除用户信息
public int deleteUser(int id);
public int updateUserById(User user);
}
UserMapper.xml
<?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">
<mapper namespace="com.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="User">
select * from user where id = #{id} </select>
<select id="findAllUsers" resultType="User">
select * from user
</select>
</mapper>
主方法测试
public static void main(String[] args) throws IOException {
//创建
sqlSessionFactory SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
//创建usermapper对象,mybatis自动生成代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //调用UserMapper的方法 //
User user = userMapper.findUserById(1);
// System.out.println(user.getUsername());
List<User> userList = userMapper.findAllUsers();
for(User user : userList) {
System.out.println(user.getUsername());
} }
新增用户
<insert id="addUser" parameterType="User" >
<selectKey keyProperty="id" order="AFTER" resultType="int">
select LAST_INSERT_ID()
</selectKey>
insert into user(username, birthday, sex, address) values(#{username}, #{birthday}, #{sex}, #{address})
</insert>
主函数
User user = new User();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); user.setUsername("田志");
user.setSex("男");
user.setBirthday(sdf.parse("2016-11-29"));
user.setAddress("江西南昌");
userMapper.addUser(user);
System.out.println(user.getId());
sqlSession.commit();
增删改操作需要 sqlSession.commit();
删除用户
<delete id="deleteUser" parameterType="int">
delete from user where user.id = #{id}
</delete>
主函数
userMapper.deleteUser(1);
sqlSession.commit();
更新用户
<update id="updateUserById" parameterType="User">
update user set username = #{username},
birthday = #{birthday}, sex = #{sex}, address = #{address} where user.id = #{id}
</update>
主函数:
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); User user = new User();
//根据id更新用户信息
user.setId(24);
user.setUsername("张四风2");
user.setBirthday(sdf.parse("2015-01-12"));
user.setSex("女");
user.setAddress("上海黄埔");
userMapper.updateUserById(user);
//提交事务
sqlSession.commit();
模糊查找用户
<select id="findUserList" parameterType="User" resultType="User">
select * from user where user.sex = #{sex} and user.username like #{username}
</select>
主函数:
User user = new User();
user.setSex("1"); user.setUsername("%张%");
//调用UserMapper的方法
List<User> list = userMapper.findUserList(user);
for(User u : list) {
System.out.println(u.getUsername());
}
动态 SQL
通过mybatis提供的各种标签方法实现动态拼接sql。
<select id="findUserList2" parameterType="User" resultType="User">
select * from user <!--where可以自动的去掉条件中的第一个and--> <where>
<if test="sex != null and sex != ''">
and user.sex = #{sex}
</if>
<if test="username != null">
and user.username like #{username}
</if>
</where>
</select>
测试代码:因为设置了动态的sql,如果不设置某个值,那么条件就不会拼接在sql上
所以我们就注释掉设置username的语句
User user = new User();
// user.setSex("1");
user.setUsername("%张%");
//调用UserMapper的方法
List<User> list = userMapper.findUserList2(user);
for(User u : list) {
System.out.println(u.getUsername());
}
多表连接
创建一个订单信息表
CREATE TABLE `orders` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT '下单用户id', `number`
varchar(32) NOT NULL COMMENT '订单号', `createtime` datetime NOT
NULL COMMENT '创建订单时间', `note` varchar(100) DEFAULT NULL
COMMENT '备注', PRIMARY KEY (`id`), KEY `FK_orders_1` (`user_id`),
CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES
`user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION )
ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- --
-------------------------- -- Records of orders -- ---------------------------- INSERT
INTO `orders` VALUES ('3', '1', '1000010', '2015-02-04 13:22:35', null);
INSERT INTO `orders` VALUES ('4', '1', '1000011', '2015-02-03 13:22:41',
null); INSERT INTO `orders` VALUES ('5', '10', '1000012', '2015-02-12
16:13:23', null);
使用Map和List集合
public List<Map<String,Object>> getAllOrderInfo();
xml
<select id="getAllOrderInfo" resultType="map">
select orders.number,user.username from orders join user on orders.user_id = user.id
</select>
遍历数组
List<Map<String,Object>> mapList = userMapper.getAllOrderInfo();
for(Map<String,Object> map : mapList) {
System.out.println("---------------");
for(Map.Entry<String,Object> entry : map.entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
}