标签:driver pool data int 项目结构 not value ica rgs
首先在pom.xml添加依赖
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.1.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.0.Final</version> </dependency>
项目结构:
创建图书实体类:
package the_template_data_jpa; public class Book { private int id; private String bookname; private float price; public Book(int id, String bookname, float price) { this.id = id; this.bookname = bookname; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Book{" + "id=" + id + ", bookname=‘" + bookname + ‘\‘‘ + ", price=" + price + ‘}‘; } }
创建BookDAO
package the_template_data_jpa; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.stereotype.Repository; import org.springframework.transaction.support.TransactionTemplate; import java.util.List; import java.util.Map; @Repository public class BookDAO { @Autowired private JdbcOperations jdbcOperations; public List<Map<String, Object>> getToList() { List<Map<String, Object>> maps = jdbcOperations.queryForList("select * from book where id > 0"); return maps; } public Map<String, Object> getToMap() { String sql = "select * from book where id > ?;"; Map<String, Object> ret = jdbcOperations.queryForMap(sql,2); return ret; } public Book getToBook() { String sql = "select * from book where id > 2;"; Book book = jdbcOperations.queryForObject( sql, (rs, rowNum) -> new Book(rs.getInt(1), rs.getString(2), rs.getFloat(3)) ); return book; } public void create(String bookname, int price) { String sql = " insert into book (bookname,price) values (?,?)"; int javaWeb = jdbcOperations.update(sql, bookname, price); if (javaWeb > 0) { System.out.println("数据插入成功"); } } }
JDBC配置
package the_template_data_jpa; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; import java.beans.PropertyVetoException; @Configuration public class JDBCTemplateConfig { @Bean DataSource dataSource (Environment env) throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(env.getProperty("jdbc.driver")); dataSource.setJdbcUrl(env.getProperty("jdbc.url")); dataSource.setUser(env.getProperty("jdbc.user")); dataSource.setPassword(env.getProperty("jdbc.password")); return dataSource; } @Bean JdbcTemplate jdbcTemplate (DataSource dataSource){ return new JdbcTemplate(dataSource); } }
配置:扫描
package the_template_data_jpa;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration //声明当前配置类
@ComponentScan(basePackages = "the_template_data_jpa") // 扫描当前包 使用 spring 注解
@PropertySource("classpath:application.properties")//加载 资源文件
@Import({JDBCTemplateConfig.class})//扫描 使用 jpa 注解的接口
public class SpringConfig {
}
测试:
package the_template_data_jpa;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext Ioc = new AnnotationConfigApplicationContext(SpringConfig.class);
BookDAO bean = Ioc.getBean(BookDAO.class);
List<Map<String, Object>> toList = bean.getToList();
System.out.println(toList);
}
}
结果:
Spring_data_jpa以及Hibernate简单使用
标签:driver pool data int 项目结构 not value ica rgs
原文地址:https://www.cnblogs.com/nongzihong/p/10167294.html