1.使用IDEA创建Maven工程
2.IDAE配置Maven
准备数据源
1 # 删除mybatis_demo数据库 2 drop database if exists mybatis_demo; 3 4 # 创建mybatis_demo数据库 5 create database mybatis_demo; 6 7 # 使用mybatis_demo数据库 8 use mybatis_demo; 9 10 # 创建account表 11 create table user ( 12 id int auto_increment primary key, 13 username varchar(20), 14 age int, 15 score int 16 ); 17 18 # 新增数据 19 insert into user (id, username, age, score) values 20 (1,'peter', 18, 100), (2,'pedro', 24, 200), 21 (3,'jerry', 28, 500), (4,'mike', 12, 300), 22 (5,'tom', 27, 1000);
JDBC连接及操作数据库 引入MySQL驱动包
1 <dependencies> 2 <!-- https://mvnrepository.com/artifact/mysql/mysql-3connector-java --> 4 <dependency> 5 <groupId>mysql</groupId> 6 <artifactId>mysql-connector-java</artifactId> 7 <version>8.0.23</version> 8 </dependency> 9 </dependencies>
刷新maven等待自动下载
代码实现
1 public class JDBCDemo { 2 public static void main(String[] args) throws Exception { 3 //1.注册驱动 4 Class.forName("com.mysql.cj.jdbc.Driver"); 5 //2.获取连接 6 Connection connection = 7DriverManager.getConnection("jdbc:mysql://localhost:3306/mybati8 s_demo", "root", "root"); 9 //3.获取Statement对象 10 PreparedStatement preparedStatement = 11connection.prepareStatement("select * from user WHERE id = 12?"); 13 preparedStatement.setInt(1, 1); 14 //4.执行SQL语句返回结果集 15 ResultSet resultSet = 16preparedStatement.executeQuery(); 17 //5.遍历结果集 18 while (resultSet.next()) { 19 System.out.println("username: " + 20resultSet.getString("username")); 21 System.out.println("age: " + 22resultSet.getString("age")); 23 } 24 //6.释放资源 25 resultSet.close(); 26 preparedStatement.close(); 27 connection.close(); 28 } 29 }
执行结果
mybatis:MyBatis基础包
logback-classic:日志依赖
分别导入这两个包
1 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 2 <dependency> 3 <groupId>org.mybatis</groupId> 4 <artifactId>mybatis</artifactId> 5 <version>3.5.6</version> 6 </dependency> 7 <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> 8 <dependency> 9 <groupId>ch.qos.logback</groupId> 10 <artifactId>logback-classic</artifactId> 11 <version>1.3.0-alpha5</version> 12 <scope>test</scope>
刷新maven等待自动下载
新建编程式配置文件:StartNoXml.java
1 @SuppressWarnings({"SqlResolve", "SqlNoDataSourceInspection", "Duplicates"}) 2 public class StartNoXml { 3 public static void main(String[] args) throws SQLException 4 { 5 // 准备jdbc事务类 6 JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory(); 7 // 配置数据源 8 PooledDataSource dataSource = new PooledDataSource( 9 "com.mysql.cj.jdbc.Driver", 10 "jdbc:mysql://localhost:3306/mybatis_demo?useSSL=false", 11 "root", 12 "root"); 13 // 配置环境,向环境中指定环境id、事务和数据源 14 Environment environment = new Environment.Builder("development") 15 .transactionFactory(jdbcTransactionFactory) 16 .dataSource(dataSource).build(); 17 // 新建 MyBatis 配置类 18 Configuration configuration = new Configuration(environment); 19 // 得到 SqlSessionFactory 核心类 20 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); 21 // 开始一个 sql 会话 22 SqlSession session = sqlSessionFactory.openSession(); 23 // 得到 sql 连接并运行 sql 语句 24 PreparedStatement preStatement = session 25 .getConnection() 26 .prepareStatement("SELECT * FROM user WHERE id = ?"); 27 preStatement.setInt(1, 1); 28 ResultSet result = preStatement.executeQuery(); 29 // 验证结果 30 while (result.next()) { 31 System.out.println("username: " + result.getString("username")); 32 System.out.println("age: " + result.getString("age")); 33 } 34 // 关闭会话 35 session.close(); 36 } 37 }