继上一篇文章,主要是在xml中配置需要继承JdbcDaoSupport类的一系列dao层的配置,下面记录下通过注解操作数据库的方法:
方法一:
TestConnMysqlDataSource.java
在class前面的@Component一定不能少;
在xml中只需要配置数据源属性就行,然后再在java中创建JdbcTemplate,继而操作数据库
<!-- 加载内网系统配置 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:sys.properties</value>
</list>
</property>
</bean>
<!-- mybatis 配置及jdbc 配置 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
package com.mdl.excel.mysql.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;
import com.mdl.excel.mysql.bean.User;
@Component
public class TestConnMysqlDataSource{
@Autowired
private DriverManagerDataSource dataSource;
public static void main(String[] args) {
// //或者ClassPathXmlApplicationContext
// ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:excel-servlet.xml");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:jdbc-servlet.xml");
TestConnMysqlDataSource icm = ac.getBean(TestConnMysqlDataSource.class);
User user = icm.Test();
System.out.println(user.getUsername() + "---" + user.getEmail());
}
private User Test(){
String sql="select * from user where id=?";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return jdbcTemplate.queryForObject(sql, new UserRowMapper(), 1);
}
class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setUsername(rs.getString("username"));
user.setEmail(rs.getString("email"));
return user;
}
}
}
方法二:
TestConnMysqlJdbcTemplate.java
在xml中直接配置好jdbcTemplate,然后再java中通过spring注解直接注入调用,与上面方法相比,在此xml中需要多添加一个jdbcTemplate的配置
<!-- 加载内网系统配置 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:sys.properties</value>
</list>
</property>
</bean>
<!-- mybatis 配置及jdbc 配置 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
package com.mdl.excel.mysql.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import com.mdl.excel.mysql.bean.User;
@Component
public class TestConnMysqlJdbcTemplate {
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:jdbc-servlet.xml");
TestConnMysqlJdbcTemplate icm = ac.getBean(TestConnMysqlJdbcTemplate.class);
User user = icm.Test();
System.out.println(user.getUsername() + "---" + user.getEmail());
}
private User Test() {
String sql = "select * from user where id=?";
return jdbcTemplate.queryForObject(sql, new UserRowMapper(), 1);
}
class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setUsername(rs.getString("username"));
user.setEmail(rs.getString("email"));
return user;
}
}
}