В этом руководстве мы расширим последний пример Привет, мир Maven + Spring , добавив поддержку JDBC, чтобы использовать Spring + JDBC для вставки записи в таблицу клиентов.
1. Таблица клиентов
В этом примере мы используем базу данных MySQL.
CREATE TABLE `customer` (
`CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) NOT NULL,
`AGE` int(10) unsigned NOT NULL,
PRIMARY KEY (`CUST_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
2. Зависимость проекта
Добавить Spring и MySQL зависимости в Maven pom.xml файл.
Файл: pom.xml
7. Запустите его
package com.csharpcoderr.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.csharpcoderr.customer.dao.CustomerDAO;
import com.csharpcoderr.customer.model.Customer;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("Spring-Module.xml");
CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
Customer customer = new Customer(1, "mkyong",28);
customerDAO.insert(customer);
Customer customer1 = customerDAO.findByCustomerId(1);
System.out.println(customer1);
}
}
выход
Customer [age=28, custId=1, name=mkyong]
Скачать исходный код
Загрузить его - SpringJDBCExample.zip (10 КБ) интеграция jdbc maven spring
0.00 (0%) 0 votes









