java spring 连接 mysql 数据库
2018-08-25 本文已影响0人
Toomi
在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对象
-
src/main/java
下创建跑package,名为com.best.domain
- 在上一步创建的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接口
-
src/main/java
下创建跑package,名为com.best.domain.repository
- 在上一步创建的package下创建一个interface,名为
CustomerRepository
内容如下:
package com.best.domain.repository;
import com.best.domain.Customer;;
public interface CustomerRepository {
public Customer find(int cust_id);
}
实现repository层CustomerRepository接口
-
src/main/java
下创建跑package,名为com.best.domain.repository.impl
- 在上一步创建的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
-
src/main/java
下创建跑package,名为com.best.config
- 在上一步创建的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的年龄