mybatis整理笔记

时间:2022-08-14 05:08:33

以下是idea2018辑编器

新建 Maven工程

1  file ->new ->project

mybatis整理笔记

新建后编程器在右下角加载插件。,这个时候需要会儿,  加载好后,软件目录会多一个ja包

类似于:

mybatis整理笔记

为了测试数据库,需要建一个表,

mybatis整理笔记

然后新建查询方式:

CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime default NULL COMMENT '生日',
`sex` char(1) default NULL COMMENT '性别',
`address` varchar(256) default NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

新建一个表,,在通过命令添加数据:

nsert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (41,'老王','2018-02-27 17:47:08','男','北京'),(42,'小二王','2018-03-02 15:09:37','女','北京金燕龙'),(43,'小二王','2018-03-04 11:34:34','女','北京金燕龙'),(45,'传智播客','2018-03-04 12:04:06','男','北京金燕龙'),(46,'老王','2018-03-07 17:37:26','男','北京'),(48,'小马宝莉','2018-03-08 11:44:00','女','北京修正');

然后通主包配置加载所需插件:

在pom.xml 中添加:  

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId>
<artifactId>day01_eesy_01mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency> </dependencies> </project>

其中解释如下:

mybatis整理笔记

如此以后会自动安装上面所涉及到的包有工具库  以上前期工作完成

第二步

开始新建模型  orm  所谓模型就是给对象一样的字段  在src -main-java  中新建一个包  com.itheima-domain

在里面建一个类user

mybatis整理笔记

详细模型代码如下:

 1 package com.itheima.domain;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 public class User implements Serializable {
7 private Integer id;
8 private String username;
9 private Date birthday;
10 private String sex;
11 private String address;
12
13 public Integer getId() {
14 return id;
15 }
16
17 public void setId(Integer id) {
18 this.id = id;
19 }
20
21 public String getUsername() {
22 return username;
23 }
24
25 public void setUsername(String username) {
26 this.username = username;
27 }
28
29 public Date getBirthday() {
30 return birthday;
31 }
32
33 public void setBirthday(Date birthday) {
34 this.birthday = birthday;
35 }
36
37 public String getSex() {
38 return sex;
39 }
40
41 public void setSex(String sex) {
42 this.sex = sex;
43 }
44
45 public String getAddress() {
46 return address;
47 }
48
49 public void setAddress(String address) {
50 this.address = address;
51 }
52
53 @Override
54 public String toString() {
55 return "User{" +
56 "id=" + id +
57 ", username='" + username + '\'' +
58 ", birthday=" + birthday +
59 ", sex='" + sex + '\'' +
60 ", address='" + address + '\'' +
61 '}';
62 }
63 }

然后配置主配置,关于数据库和资源的

主配置文件内容如下:

<?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>
<!--主配置-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="username" value="root"/>
<property name="password" value="142857"/>
</dataSource>
</environment>
</environments>
<!--yanse-->
<mappers>
<mapper resource="com/itheima/dao/IUserDao.xml"/>
</mappers> </configuration>

mybatis整理笔记

然后一个查询配置,代码如下:src-main-java 新建包  com.itheima.dao   IUserDao.xml

package com.itheima.dao;
import com.itheima.domain.User;
import java.util.List; public interface IUserDao {
List<User> findAll(); }

开始测试

resources类下新建测试  com.itheima.dao.text

MybaisTest

package com.itheima.test;

import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
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 java.io.InputStream;
import java.util.List; public class MybatisTest { public static void main(String[] args)throws Exception {
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产SqlSession对象
SqlSession session = factory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
IUserDao userDao = session.getMapper(IUserDao.class);
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for(User user : users){
System.out.println(user);
}
//6.释放资源
session.close();
in.close();
}
}

完成第一个dome