spring-jdbcTemplate

原来都是各种util , spring这里提供操作jdbc的工具,该类,不叫util,他叫模板。

jdbcTemplate的基本使用

image-20201129152313549

spring-tx是transaction

image-20201129152448305

配置

<<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>5.0.5.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-txartifactId>
    <version>5.0.5.RELEASEversion>
dependency>
@Test
public void test() throws PropertyVetoException {
    // 创建数据源
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ustc");
    dataSource.setUser("root");
    dataSource.setPassword("yyyyyyyy");

    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);

    int row = jdbcTemplate.update("insert into account values(?,?)","xiaoli","50000");
    System.out.println(row);
}
//输出1  ,原来account表空

spring核心是ioc

image-20201129154715991

//配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSouce" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSouce"/>
    bean>
beans>

context命名空间的需要,后期上面3个配置常用。

增删改都是update方法,改SQL就行.

@RunWith(SpringJUnit4ClassRunner.@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateTest {
    @Autowired
    private JdbcTemplate mJdbcTemplate;

    //delete
    @Test
    public void testAdd(){
        int res = mJdbcTemplate.update("delete from account where name = ?","xiaoli");
        System.out.println(res);
    }
    //update
    @Test
    public void testModify(){
        int res = mJdbcTemplate.update("update  account set money = ? where name = ?",3000,"xiaoli");
        System.out.println(res);
    }
    //query all
    @Test
    public void testQueryAll(){
        List accountList = mJdbcTemplate.query("select * from  account ", new BeanPropertyRowMapper(Account.class));
        System.out.println(accountList);
    }
    //query one
    @Test
    public void testQueryOne(){
        Account xiaoli = mJdbcTemplate.queryForObject("select * from  account where name = ?", new BeanPropertyRowMapper(Account.class), "xiaoli");
        System.out.println(xiaoli);
    }
    //聚合查询
    //query 总数
    @Test
    public void testQueryCount(){
        int xiaoli = mJdbcTemplate.queryForObject("select count(*) from  account",Integer.class);
        System.out.println(xiaoli);
    }
}

查一个,查多个,聚合查出来的是基本数值的。

image-20201129164622214


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!