目录在创建时:com.itheima.dao是一级目录
第三个:mybatis的映射配置文件位置必须和dao接口的包结构相同
第四个:映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名
第五个:映射配置文件的操作配置(select),id属性的取值必须是dao接口的方法名
注意:当我们遵从了第三,四,五点之后,我们在开发中就无须再写dao的实现类。**
1、创建一个新的maven项目
目录结构*
最后完成的目录结构如下:
2、在pom.xml中导入相关的依赖
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”>
4.0.0
com.keafmd
day01_eesy_01mybatis
1.0-SNAPSHOT
jar
org.mybatis
mybatis
3.5.5
mysql
mysql-connector-java
5.1.49
log4j
log4j
1.2.17
junit
junit
4.13
test
3、准备数据库
创建个数据库eesy_mybatis
,入门案例中只使用到了user表。
sql文件:
DROP TABLE IF EXISTS user
;
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;
insert 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’,‘女’,‘北京修正’);
DROP TABLE IF EXISTS account
;
CREATE TABLE account
(
ID
int(11) NOT NULL COMMENT ‘编号’,
UID
int(11) default NULL COMMENT ‘用户编号’,
MONEY
double default NULL COMMENT ‘金额’,
PRIMARY KEY (ID
),
KEY FK_Reference_8
(UID
),
CONSTRAINT FK_Reference_8
FOREIGN KEY (UID
) REFERENCES user
(id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into account
(ID
,UID
,MONEY
) values (1,41,1000),(2,45,1000),(3,41,2000);
DROP TABLE IF EXISTS role
;
CREATE TABLE role
(
ID
int(11) NOT NULL COMMENT ‘编号’,
ROLE_NAME
varchar(30) default NULL COMMENT ‘角色名称’,
ROLE_DESC
varchar(60) default NULL COMMENT ‘角色描述’,
PRIMARY KEY (ID
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into role
(ID
,ROLE_NAME
,ROLE_DESC
) values (1,‘院长’,‘管理整个学院’),(2,‘总裁’,‘管理整个公司’),(3,‘校长’,‘管理整个学校’);
DROP TABLE IF EXISTS user_role
;
CREATE TABLE user_role
(
UID
int(11) NOT NULL COMMENT ‘用户编号’,
RID
int(11) NOT NULL COMMENT ‘角色编号’,
PRIMARY KEY (UID
,RID
),
KEY FK_Reference_10
(RID
),
CONSTRAINT FK_Reference_10
FOREIGN KEY (RID
) REFERENCES role
(ID
),
CONSTRAINT FK_Reference_9
FOREIGN KEY (UID
) REFERENCES user
(id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into user_role
(UID
,RID
) values (41,1),(45,1),(41,2);
4、编写User实体类
User类:
package com.keafmd.domain;
import java.util.Date;
/**
-
Keafmd
-
@ClassName: User
-
@Description: user实体类
-
@author: 牛哄哄的柯南
-
@date: 2021-02-06 19:26
*/
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return “User{” +
“id=” + id +
“, username='” + username + ‘’’ +
“, birthday=” + birthday +
“, sex='” + sex + ‘’’ +
“, address='” + address + ‘’’ +
‘}’;
}
}
5、编写持久层接口 IUserDao
IUserDao:
package com.keafmd.dao;
import com.keafmd.domain.User;
import java.util.List;
/**
-
Keafmd
-
@ClassName: IUserDao
-
@Description: 用户的持久层接口
-
@author: 牛哄哄的柯南
-
@date: 2021-02-06 19:29
*/
public interface IUserDao {
/**
-
查询所有操作
-
@return
*/
List findAll();
}
6、编写持久层接口的映射文件 IUserDao.xml
**要求:
创建位置:必须和持久层接口在相同的包中。
名称:必须以持久层接口名称命名文件名,扩展名是.xml**
IUserDao.xml:
<?xml version="1.0" encoding="UTF-8"?>select * from user
7、编写 SqlMapConfig.xml 配置文件
SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>8、引入log4j.properties文件
log4j.properties:
Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
9、编写测试类
**第一步:读取配置文件
第二步:创建SqlSessionFactory工厂
第三步:创建SqlSession
第四步:创建Dao接口的代理对象
第五步:执行dao中的方法
第六步:释放资源**
MybatisTest:
package com.keafmd.test;
import com.keafmd.dao.IUserDao;
import com.keafmd.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 javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
/**
-
Keafmd
-
@ClassName: MybatisTest
总结
面试前要精心做好准备,简历上写的知识点和原理都需要准备好,项目上多想想难点和亮点,这是面试时能和别人不一样的地方。
还有就是表现出自己的谦虚好学,以及对于未来持续进阶的规划,企业招人更偏爱稳定的人。
万事开头难,但是程序员这一条路坚持几年后发展空间还是非常大的,一切重在坚持。
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
前端面试题汇总
JavaScript
前端资料汇总