各位好,我是小饼干,一个编程界的小学生,每天都希望学习一点点编程相关。Mybatis是一款优秀持久层框架,用于简化JDBC开发,本文是通过一个案例来对Mybatis进行快速入门,感谢各位的驻足,希望能得到一个小小的支持,比如说♥。
参考代码,如果对mysql不理解,可以参考这几篇文章Mysql下载与卸载 ,Mysql基础 ,MySQL高级
create database mybatis;-- 创建数据库
use mybatis;
drop table if exists tb_user;-- 创建表格
create table tb_user(
id int primary key auto_increment,
username varchar(20),
password varchar(20),
gender char(1),
addr varchar(30)
);
//添加数据
INSERT INTO tb_user VALUES (1, 'zhangsan', '123', '男', '北京');
INSERT INTO tb_user VALUES (2, '李四', '234', '女', '天津');
INSERT INTO tb_user VALUES (3, '王五', '11', '男', '西安');
在IDEA中创建Maven模块,命名为mybatis-demo。在pom.xml文件中导入坐标
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.5version>
dependency>
还有一些其他配置这里对于每个人都一样,整理一下以后都粘贴复制就好
(以下是本次项目需要的,都要放在
标签下)
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.32version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.20version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
在src->main->resources创建lodback.xml
文件,
写入以下代码
<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %npattern>
encoder>
appender>
<logger name="com.itheima" level="DEBUG" additivity="false">
<appender-ref ref="Console"/>
logger>
<root level="DEBUG">
<appender-ref ref="Console"/>
root>
configuration>
其实就是在src->main->resources创建mybatis-config.xml
文件,
将以下代码贴入
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
mappers>
configuration>
其中这几个地方要改变数值(这块涉及到JDBC和mysql内容)
-
我按照我的MySQL改成了以下内容
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
将以下代码贴入该文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
select>
mapper>
该代码中有两个地方要处理
<mapper namespace="testt">
<select id="selectAll" resultType="com.zaizai.pojo.User">
select * from tb_user;
select>
mapper>
//alt+鼠标左键
public class User {
private Integer id ;
private String username ;
private String password ;
private String gender ;
private String addr ;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", addr='" + addr + '\'' +
'}';
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
public class MybatisDemo {
public static void main(String[] args) throws Exception {
//1.加载mybatis的核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession的对象。用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql语句
List<User> users = sqlSession.selectList("testt.selectAll");
System.out.println(users);
sqlSession.close();
}
}
第一步中在官网上有说明,而且这一步需要抛出异常
最后的最后,希望大家都拥有学习知识和解决问题的快乐。