美文网首页
java spring 连接 mysql 数据库

java spring 连接 mysql 数据库

作者: Toomi | 来源:发表于2018-08-25 23:50 被阅读0次

    源码下载

    在mysql数据库创建表CUSTOMER

    CREATE TABLE CUSTOMER(CUST_ID  INT(11),
                  NAME VARCHAR(60),
                  AGE INT(11),
                  PRIMARY KEY (`CUST_ID`)
                  );
    

    插入示例数据

       INSERT INTO CUSTOMER VALUES(1,'Toomi',23);
    

    在eclipse新建maven project

    File --> New --> Maven Project
    - archetype: maven-archetype-quickstart
    - Group Id: come.best
    - Artifact Id: MyJdbcApp
    - 其他默认

    pom.xml添加如下依赖

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
    

    建立domain层customer对象

    1. src/main/java下创建跑package,名为 com.best.domain
    2. 在上一步创建的package下创建一个class,名为Customer

    内容如下:

    package com.best.domain;
    
    public class Customer {
        int custId;
        String name;
        int age;
        
        public Customer() {
            super();
        }
        
        public Customer(int custId, String name, int age) {
            this.custId = custId;
            this.name = name;
            this.age = age;
        }
    
        public int getCustId() {
            return custId;
        }
    
        public void setCustId(int custId) {
            this.custId = custId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Customer [age=" + age + ", custId=" + custId + ", name=" + name + "]";
        }
    
    }
    

    创建repository层CustomerRepository接口

    1. src/main/java下创建跑package,名为 com.best.domain.repository
    2. 在上一步创建的package下创建一个interface,名为CustomerRepository

    内容如下:

        package com.best.domain.repository;
        import com.best.domain.Customer;;
        
        public interface CustomerRepository {
            public Customer find(int cust_id);
        }
    

    实现repository层CustomerRepository接口

    1. src/main/java下创建跑package,名为 com.best.domain.repository.impl
    2. 在上一步创建的package下创建一个class,名为CustomerRepositoryImpl

    内容如下:

        package com.best.domain.repository.impl;
        
        import com.best.domain.Customer;
        import com.best.domain.repository.*;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import org.springframework.jdbc.core.JdbcTemplate;
        import org.springframework.jdbc.core.RowMapper;
        
        public class CustomerRepositoryImpl implements CustomerRepository {
            private JdbcTemplate jdbcTemplate;
        
            public JdbcTemplate getJdbcTemplate() {
                return jdbcTemplate;
            }
        
            private static final class CustomerRowMapper implements RowMapper<Customer> {
                public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Customer customer = new Customer(rs.getInt("CUST_ID"), rs.getString("NAME"), rs.getInt("Age"));
                    return customer;
                }
            }
        
            public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
                this.jdbcTemplate = jdbcTemplate;
            }
        
            public Customer find(int cust_id) {
        
                return jdbcTemplate.queryForObject("SELECT CUST_ID,NAME,AGE FROM CUSTOMER WHERE CUST_ID = ?",
                        new CustomerRowMapper(), cust_id);
            }
        }
    
    

    配置dataSource以及JdbcTemplate

    1. src/main/java下创建跑package,名为 com.best.config
    2. 在上一步创建的package下创建一个class,名为SpringJdbcConfig

    内容为:

        package com.best.config;
        
        import org.springframework.context.annotation.Configuration;
        import org.springframework.context.annotation.Bean;
        import org.springframework.jdbc.datasource.DriverManagerDataSource;
        import org.springframework.jdbc.core.JdbcTemplate;
        import javax.sql.DataSource;
        import com.best.domain.repository.*;
        import com.best.domain.repository.impl.CustomerRepositoryImpl;
        
        @Configuration
        public class SpringJdbcConfig {
            @Bean
            public DataSource dataSource() {
                DriverManagerDataSource dataSource = new DriverManagerDataSource();
                dataSource.setDriverClassName("com.mysql.jdbc.Driver");
                dataSource.setUrl("jdbc:mysql://localhost:3306/toomi");
                dataSource.setUsername("toomi");
                dataSource.setPassword("toomi");
        
                return dataSource;
            }
        
            @Bean
            public JdbcTemplate jdbcTemplate() {
                JdbcTemplate jdbcTemplate = new JdbcTemplate();
                jdbcTemplate.setDataSource(dataSource());
                return jdbcTemplate;
            }
        
            @Bean
            public CustomerRepository customerRepository() {
                CustomerRepositoryImpl customerRepository = new CustomerRepositoryImpl();
                customerRepository.setJdbcTemplate(jdbcTemplate());
                return customerRepository;
        
            }
        }
    

    修改程序入口 App.java

    将package com.best.MyJdbcApp下App.java文件内容修改如下

        package com.best.MyJdbcApp;
        
        import java.sql.SQLException;
        import org.springframework.context.annotation.AnnotationConfigApplicationContext;
        import com.best.config.SpringJdbcConfig;
        import com.best.domain.Customer;
        import com.best.domain.repository.CustomerRepository;
        
        public class App {
            public static void main(String[] args) throws SQLException {
                AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
                        SpringJdbcConfig.class);
                CustomerRepository customerRepository = applicationContext.getBean(CustomerRepository.class);
                Customer customer = customerRepository.find(1);
                System.out.print(customer.getAge());
                //applicationContext.close();
            }
        }
    

    运行程序

    选中 App.java --> 右键 Run As --> Java Application
    将会在控制台看见 输出数字23,即toomi的年龄

    参考链接

    相关文章

      网友评论

          本文标题:java spring 连接 mysql 数据库

          本文链接:https://www.haomeiwen.com/subject/uzsniftx.html