Spring Template应用案例源代码:
MVDem
o.zip
20.72KB
***1-加载MAVEN依赖
还导一个JDBC的,下面没展示
***2-编写Spring配置文件
首先确定resource文件夹是资源根目录
新建spring的配置文件
编写配置文档
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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:component-scan base-package="com.company"></context:component-scan> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!--配置spring的jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans> |
***3-编写Dao接口及实现类(运用JdbcTemplate实现,简化JDBC原型代码)
运用JAVA8新特性改写代码如下
***4-测试