JDBC Template

一、配置

1、在maven的pom.xml中配置spring-jdbc。

2、创建一个spring.xml。进行bean管理,创建两个bean,一个bean进行数据库的连接,另一个bean将连接好的数据库交给jdbcTemplate管理。

例如:

1 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
2     <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
3     <property name="url" value="jdbc:mysql://localhost:3306/selection_course?useSSL=false&amp;serverTimezone=GMT&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
4     <property name="username" value="root"/>
5     <property name="password" value="61154852a"/>
6 </bean>
7 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
8     <property name="dataSource" ref="dataSource"/>
9 </bean>

二、使用

1、在当前类中引入applicationContext(spring.xml)配置文件并创建一个对象

2、使用jdbcTemplate引入bean

3、使用JdbcTemplate对象使用对象的方法执行sql语句

例如:

1  ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
2         JdbcTemplate jdbcTemplate=(JdbcTemplate)context.getBean("jdbcTemplate");
3         jdbcTemplate.execute("create table user1(id int,name varchar (20))");

 三、具体使用方法

 

增删改查:    https://blog.csdn.net/abc997995674/article/details/80183597 

posted @ 2019-05-12 22:12  鼠小小  阅读(122)  评论(0编辑  收藏  举报