Mybatis工程搭建
1.chuangjianMavengongcheng(为什么审核不通过)
新建Maven项目名为“mybatis-demo“,设置好Maven版本、配置文件以及Maven仓库
2.SQLshjuyuanzhunbei(为什么审核不通过)
# 删除mybatis_demo数据库
drop database if exists mybatis_demo;
# 创建mybatis_demo数据库
create database mybatis_demo;
# 使用mybatis_demo数据库
use mybatis_demo;
# 创建account表
create table user (
id int auto_increment primary key,
username varchar(20),
age int,
score int
);
# 新增数据
insert into user (id, username, age, score) values
(1,'peter', 18, 100), (2,'pedro', 24, 200),
(3,'jerry', 28, 500), (4,'mike', 12, 300),
(5,'tom', 27, 1000);
3.yinruMySQLqudongbao(为什么审核不通过)
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
shuaxin maven dengdaizidongxiazai(为什么审核不通过)
4.创建JDBCDemo(为什么审核不通过)
daimashixian:
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis_demo", "root", "root");
//3.获取Statement对象
PreparedStatement preparedStatement = connection.prepareStatement("select * from user WHERE id = ?");
preparedStatement.setInt(1, 1);
//4.执行SQL语句返回结果集
ResultSet resultSet = preparedStatement.executeQuery();
//5.遍历结果集
while (resultSet.next()) {
System.out.println("username: " + resultSet.getString("username"));
System.out.println("age: " + resultSet.getString("age"));
}
//6.释放资源
resultSet.close();
preparedStatement.close();
connection.close();
}
}
yunxinjieguo(为什么审核不通过)
5.引入MyBatis依赖(为什么审核不通过)
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha5</version>
<scope>test</scope>
</dependency>
mybatis:MyBatis基础包(为什么审核不通过)
logback-classic:日志依赖(为什么审核不通过)
编程式配置方法
6.xinjianpeizhiwenjian:StartNoXml.java(为什么审核不通过)
@SuppressWarnings({"SqlResolve", "SqlNoDataSourceInspection", "Duplicates"})
public class StartNoXml {
public static void main(String[] args) throws SQLException {
// 准备jdbc事务类
JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
// 配置数据源
PooledDataSource dataSource = new PooledDataSource(
"com.mysql.cj.jdbc.Driver",
"jdbc:mysql://localhost:3306/mybatis_demo?useSSL=false",
"root",
"root");
// 配置环境,向环境中指定环境id、事务和数据源
Environment environment = new Environment.Builder("development")
.transactionFactory(jdbcTransactionFactory)
.dataSource(dataSource).build();
// 新建 MyBatis 配置类
Configuration configuration = new Configuration(environment);
// 得到 SqlSessionFactory 核心类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
// 开始一个 sql 会话
SqlSession session = sqlSessionFactory.openSession();
// 得到 sql 连接并运行 sql 语句
PreparedStatement preStatement = session
.getConnection()
.prepareStatement("SELECT * FROM user WHERE id = ?");
preStatement.setInt(1, 1);
ResultSet result = preStatement.executeQuery();
// 验证结果
while (result.next()) {
System.out.println("username: " + result.getString("username"));
System.out.println("age: " + result.getString("age"));
}
// 关闭会话
session.close();
}
}
yunxinjieguo(救救孩儿吧)
配置文件配置mybatis
7.resources文件夹下xinjianpeizhiwenjian:mybatis-config.xml(救救孩儿吧)
<?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="development">
<environment id="development">
<!-- 事务管理器 -->
<transactionManager type="JDBC"/>
<!-- 数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_demo?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
8.xinjianqidonglei:StartWithXml.java(救救孩儿吧)
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.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@SuppressWarnings({"SqlResolve", "SqlNoDataSourceInspection", "Duplicates"})
public class StartWithXml {
public static void main(String[] args) throws IOException, SQLException {
// 读取配置文件
InputStream configuration = Resources.getResourceAsStream("mybatis-config.xml");
// 得到 SqlSessionFactory 核心类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
// 开始一个 sql 会话
SqlSession session = sqlSessionFactory.openSession();
// 得到 sql 连接并运行 sql 语句
PreparedStatement preStatement = session
.getConnection()
.prepareStatement("SELECT * FROM user WHERE id = ?");
preStatement.setInt(1, 1);
ResultSet result = preStatement.executeQuery();
// 验证结果
while (result.next()) {
System.out.println("username: " + result.getString("username"));
System.out.println("age: " + result.getString("age"));
}
// 关闭会话
session.close();
}
}
yuncinjieguo(救救孩儿吧)
(救救孩儿吧)