一、结构体查看
1、dao结构
2、mapper结构
注意事项:
有mapper没有dao.
第一种是用自己起的namespace.方法名去调用,第二种是利用反射创建接口对象.方法名去调用
通用步骤
1、导包
mybatis-3.2.7 核心包
mybatisORM-1.0-SNAPSHOP 同学自己写的mybatis框架,就是这个不要
junit-4.9 单元测试包,我都用main方法测试的,也可以不要
2、写SqlMapConfig.xml
标注:
都在src下面(王八的屁股)
sqlMapConfig:
<?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 环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<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>
</configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/kj?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456
(1) SqlMapConfig命名都可以,但是大家约定俗成命名为“SqlMapConfig.xml”
(2) <configuration>标签之前是头标签,也是引用约束,直接复制就行
(3) <properties> 是引用properties 文件,里面我写的是数据库的基本配置信息
(4)<environments> 配置myBatis的环境信息(基本都在里面配置)里面可以有多个<environment> 但每次只能生效一个;default对应的是生效的<environment>标签
(5)<environment> 环境信息的核心配置,有一个id唯一标识
(6)dataSource的name是固定的,value对应的是db里面的名
记得放log4j
3、对照数据库表写实体类
表:
CREATE TABLE `friend` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`gender` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`phone_no` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`school_name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`addr` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`bir` DATE DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 10002 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Records of friend
-- ----------------------------
INSERT INTO `friend` VALUES (1, '超帅', '男', '13400000000', '北京美妆科技有限公司', '北京市通州区', '1990-01-01');
INSERT INTO `friend` VALUES (2, '菜菜', '女', '13400010000', '淮安市北京路小学', '淮安市北京路', '1996-11-20');
INSERT INTO `friend` VALUES (3, '斌哥', '男', '13400020000', '北京市海淀区定慧里小学', '北京市海淀区', '1999-11-15');
INSERT INTO `friend` VALUES (4, '珠儿', '女', '13400030000', '北京市东城区礼士胡同小学', '北京市东城区', '2015-08-30');
INSERT INTO `friend` VALUES (5, '芷若', '女', '13400040000', '石家庄铁道学院', '石家庄铁道学', '2000-10-20');
INSERT INTO `friend` VALUES (6, '福婕', '女', '13400050000', '河北师范大学', '河北师范大学', '2004-09-08');
INSERT INTO `friend` VALUES (7, '刘佳', '女', '13400060000', '石家庄学院', '石家庄学院', '2012-07-22');
INSERT INTO `friend` VALUES (8, '郑婵', '女', '13400070000', '河北师范大学汇华学院', '河北师范大学', '1994-11-12');
上面是我用的表,你懒的建可以用
实体类:
public class Friend {
private int id;
private String name;
private String gender;
private String phoneNo;
private String schoolName;
private String addr;
private Date bir;
一、有Dao实现类
在实体类的那个包下创建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="test">
<select id="selectAll" resultType="com.zyd.pojo.Friend">
select * from friend
</select>
</mapper>
(1) mapper之前的头复制就行
(2) mapper namespace的名字记得自己起,dao实现类里面用
基础语法这里不写,加油
dao层的实现类
public class FriendDaoImpl implements FriendDao {
SqlSessionFactory sqlSessionFactory=null;
{
InputStream resources= null;
try {
resources = Resources.getResourceAsStream("SqlMapConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(resources);
}
@Override
public List<Friend> selectAll() {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Friend> selectAll = sqlSession.selectList("test.selectAll");
System.out.println(selectAll);
return selectAll;
}
public static void main(String[] args) {
FriendDaoImpl f=new FriendDaoImpl();
f.selectAll();
}
}
二、Mapper替换Dao格式
记得命名改成mapper,本人懒得
同一个包下的xml