第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

java jdbctemplate 簡單

標(biāo)簽:
Java

java jdbctemplate
1、新建java 项目
2、lib 文件夹
3、jar 包
c3p0-0.9.1.2.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

4、db.properties
user=root
password=
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/c
initialPoolSize=5
maxPoolSize=10

5、applicationContext.xml 文件 类路径下(也就是:src)

      <context:property-placeholder location="classpath:db.properties"/>

      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
             <property name="user" value="${user}" />
             <property name="password" value="${password}" />
             <property name="driverClass" value="${driverClass}" />
             <property name="jdbcUrl" value="${jdbcUrl}" />

             <property name="initialPoolSize" value="${initialPoolSize}" />
             <property name="maxPoolSize" value="${maxPoolSize}" />
      </bean>

      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
             <property name="dataSource" ref="dataSource"></property>
      </bean>

6、sql
CREATE TABLE dept (
id int(11) NOT NULL AUTO_INCREMENT,
dept_name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

CREATE TABLE employee (
id int(11) NOT NULL AUTO_INCREMENT,
employee_name varchar(255) NOT NULL,
tel varchar(255) DEFAULT NULL,
dept_id int(11) DEFAULT NULL,
PRIMARY KEY (id),
KEY dept_id (dept_id),
CONSTRAINT dept_id FOREIGN KEY (dept_id) REFERENCES dept (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

7、简单java 类
8、Test类

public class SpringTemplate {
      private ApplicationContext ctx = null;
      private JdbcTemplate jdbcTemplate = null;
      {
             ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
             jdbcTemplate = ctx.getBean("jdbcTemplate", JdbcTemplate.class);
      }
      // 根据条件查询一条记录
      @Test
      public void testGetData01() {
             String sql = "select id,dept_name from dept where id=?";
             RowMapper<Dept> rowMapper = new BeanPropertyRowMapper<>(Dept.class);
             Dept dept = jdbcTemplate.queryForObject(sql, rowMapper, 2);
             System.out.println(dept);
      }
      // 修改部门名称
      @Test
      public void testUpdate03() {
             String sql = "update dept set dept_name=? where id=?";
             jdbcTemplate.update(sql, "测试部", 3);
      }
      // 删除指定部门,验证mysql外键是否生效
      @Test
      public void testUpdate02() {
             String sql = "delete from dept where id=?";
             jdbcTemplate.update(sql, 1);
      }
      // 往部门表中新增一条记录
      @Test
      public void testUpdate01() {
             String sql = "insert into dept(dept_name) values(?)";
             jdbcTemplate.update(sql, "开发部");
      }
      @Test
      public void testDataSource() throws SQLException {
             DataSource dataSource = ctx.getBean(DataSource.class);
             System.out.println(dataSource.getConnection());
      }
}
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消