DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///springjdbc");
dataSource.setUsername("root");
dataSource.setPassword("1997WFY.....");
JdbcTemplate template=new JdbcTemplate();
template.setDataSource(dataSource);
template.execute("create table book(id int primary key auto_increment,name varchar(20) not null,author varchar(25))");
<!-- XML配置Spring默认的连接池 -->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///springjdbc"/>
<property name="username" value="root"/>
<property name="password" value="1997WFY....."/>
</bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="driverManagerDataSource"/>
</bean>
/**
* @author BeautifulSoup
* 首先使用Spring内置的连接池
*/
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringJdbcTest {
@Autowired
private JdbcTemplate template;
@Test
public void testDriverManagerDataSource() {
template.execute("create table book(id int primary key auto_increment,name varchar(20) not null,author varchar(25))");
}
}
<!-- 配置Druid的连接池 -->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///springjdbc" />
<property name="username" value="root" />
<property name="password" value="1997WFY....." />
<!-- 设置初始的连接数目,最小的连接数,最大的连接数 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="8" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="10000" />
<!-- 配置间隔多久才进行一次检测需要关闭的空闲连接 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小的生存时间 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
<property name="defaultAutoCommit" value="true" />
<!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
<property name="validationQuery" value="select 1 " />
<property name="filters" value="stat" />
</bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource" />
</bean>
/**
* @author BeautifulSoup
* 首先使用Spring内置的连接池
*/
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringJdbcTest {
@Autowired
private JdbcTemplate template;
@Test
public void testSpringJdbc() {
template.execute("create table book(id int primary key auto_increment,name varchar(20) not null,author varchar(25))");
}
}
/**
* @author BeautifulSoup
* 创建实体类对象
*/
public class Book {
private Integer id;
private String name;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
}
}
<bean class="com.fuyunwang.springjdbc.dao.BookDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
/**
* @author BeautifulSoup 完成基本的增删改查
*/
public class BookDao extends JdbcDaoSupport {
public void add(Book book) {
String sql = "insert into book values(?,?,?)";
getJdbcTemplate().update(sql, book.getId(), book.getName(),
book.getAuthor());
}
public void update(Book book) {
String sql = "update book set name = ? , author = ? where id =?";
getJdbcTemplate().update(sql, book.getName(), book.getAuthor(),
book.getId());
}
public void delete(Book book) {
String sql = "delete from book where id =?";
getJdbcTemplate().update(sql, book.getId());
}
public int findCount() {
String sql = "select count(*) from book";
return getJdbcTemplate().queryForInt(sql);
}
public String findNameById(int id) {
String sql = "select name from book where id = ?";
return getJdbcTemplate().queryForObject(sql, String.class, id);
}
public Book findById(int id) {
String sql = "select * from book where id = ?";
return getJdbcTemplate().queryForObject(sql, new BookMapper(), id);
}
public List<Book> findAll(){
String sql="select * from book";
return getJdbcTemplate().query(sql, new BookMapper());
}
class BookMapper implements RowMapper<Book> {
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
return book;
}
}
}
/**
* @author BeautifulSoup
* 首先使用Spring内置的连接池
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringJdbcTest {
@Autowired
private BookDao bookDao;
@Test
public void jdbcTemplateAdd(){
Book book=new Book();
book.setId(1);
book.setName("SpringBoot实战");
book.setAuthor("Craig Walls");
bookDao.add(book);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有