JdbcTemplate
JdbcTemplate就是采用AOP思想,将Jdbc操作中的模板代码全部简化,开发者只需要完成最核心的SQL以及结果的检索
使用步骤:
导入jar包
mysql相关的配置信息
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.sxt.dao.*"></context:component-scan>
<!-- 配置数据源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<!-- 配置数据库的信息 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/1227?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="1999"/>
</bean>
<!-- 配置JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="template">
<!-- 构造注入关联数据源 -->
<constructor-arg ref="dataSource"/>
</bean>
</beans>
创建工具类
package com.sxt.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.sxt.dao.UserDao;
import com.sxt.pojo.User;
@Repository
public class UserDaoImpl implements UserDao {
@Resource
private JdbcTemplate template;
@Override
public void add() {
String sql = "insert into t_user(username,password) values(?,?)";
int i = template.update(sql,"dpb","111");
System.out.println("影响的行数:"+i);
}
@Override
public void update() {
String sql = "update t_user set username=?,password=? where id=?";
int i = template.update(sql,"a123","111",4);
System.out.println("影响的行数:"+i);
}
@Override
public void delete() {
String sql = "delete from t_user where id=?";
int i = template.update(sql,14);
System.out.println("影响的行数:"+i);
}
@Override
public void query1() {
String sql = "select * from t_user";
List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
for (User user : list) {
System.out.println(user);
}
}
@Override
public void query2() {
// TODO Auto-generated method stub
}
}
测试类
package com.sxt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sxt.dao.UserDao;
public class Test1 {
public static void main(String[] args) {
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao dao = ac.getBean(UserDao.class);
//dao.add();
//dao.update();
//dao.delete();
dao.query1();
}
}
java中的乱码
1.项目编码
2. 文件本身的编码(jsp或者html的head中指定的编码)
3. GET请求乱码(修改Tomcat配置)
4. POST请求乱码(Java代码解决,可以使用过滤器统一解决)
5. 响应乱码(修改响应编码,主要是修改content-type)
6. 数据库乱码:
7. 确认乱码根源(数据库乱码还是java中乱码)
8. 通过在java中打印日志,确认乱码的根源 2. 通过在java中打印日志,确认乱码的根源
9. 第三种情况,数据在从Java到数据库实例的过程中乱码了,此时修改数据库连接地址即可解决:
dbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8