1、maven项目-----使用jdbc连接数据库demo

  1. 创建maven项目
  2. 引入mysql依赖包
    <dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.32</version>
    </dependency>

3、创建数据库及表

4、JDBC连接数据库

import java.sql.*;

/**
*采用JDBC连接数据库
 **/
public class JDBCTest {
    public static void main(String[] args) throws Exception{

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;

        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/springtest";
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);

            //获取statement preparedStatement
            String sql = "select * from user";
            preparedStatement = connection.prepareStatement(sql);

            // 执行查询
            rs = preparedStatement.executeQuery();

            // 处理结果集
            while (rs.next()) {
                System.out.print(rs.getString("uid")+" ");
                System.out.print(rs.getString("uname")+" ");
                System.out.println(rs.getString("usex"));

            }
        } finally {
            // 关闭连接,释放资源
            if (rs != null) {
                rs.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}

运行结果:

1 Tom 男
2 Jany 女
3 Mary 男 

 

 

下一篇继续学习mybatis 实现增删改查CRUD--入门级https://blog.csdn.net/LOVEYSUXIN/article/details/103464456

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MybatisX-Generator是一款能够根据数据库表结构自动生成Mybatis代码的工具,可以大大简化Mybatis的开发工作。 使用步骤如下: 1. 添加maven依赖 在项目的pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.github.mybatisx</groupId> <artifactId>mybatisx-generator-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> ``` 2. 配置generator.properties文件 在src/main/resources目录下创建generator.properties文件,并配置以下属性: ``` # 数据库连接信息 jdbc.url=jdbc:mysql://localhost:3306/mybatisx?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai jdbc.driver=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=root # 代码生成路径 target.project.dir=./src/main/java target.resources.dir=./src/main/resources # 代码包路径 package.parent=com.mybatisx.demo package.module=user package.mapper=mapper package.entity=entity package.xml=xml # 要生成的表 table.names=tb_user # 生成策略 strategy.include=tb_user strategy.super.entity.class=io.github.mybatisx.support.mapper.BaseModel strategy.super.mapper.class=io.github.mybatisx.support.mapper.BaseMapper strategy.super.service.class=io.github.mybatisx.support.service.BaseService strategy.super.serviceImpl.class=io.github.mybatisx.support.service.impl.BaseServiceImpl ``` 3. 运行MybatisXGeneratorApplication类 在src/main/java目录下创建MybatisXGeneratorApplication类,并运行main方法。 ``` @SpringBootApplication public class MybatisXGeneratorApplication { public static void main(String[] args) { SpringApplication.run(MybatisXGeneratorApplication.class, args); } } ``` 4. 生成代码 执行MybatisXGeneratorApplication的main方法,在控制台中可以看到生成的代码信息。 代码生成后,会在配置的代码生成路径下生成对应的Java文件、Mapper文件和XML文件。 以上就是mybatisx-generator的使用步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值