首先创建一个Maven Web项目
创建数据库spring并创建表customers 字段id,name,age
导入spring核心依赖、spring-jdbc依赖、mysql-connector依赖、commons-logging依赖
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
创建User实体类 并加入get/set方法(toString方法可以不加)
package com.wang.pojo;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建dao层
package com.wang.dao;
import com.wang.pojo.User;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void executeTest(){
String sql="create table temp(user_id integer,name varchar(100))";
jdbcTemplate.execute(sql);
}
public void updateInsert(User user){
String sql="insert into customers values(?,?,?)";
jdbcTemplate.update(sql,new Object[]{user.getId(),user.getName(),user.getAge()});
}
public void updateEdit(String id){
String sql="update customers set age=50 where id=?";
jdbcTemplate.update(sql,new Object[]{id});
}
public void updateDel(){
String sql="delete from customers where id=7";
jdbcTemplate.update(sql);
}
public int findNumbers(){
String sql="select count(*) from customers";
int total=jdbcTemplate.queryForObject(sql,Integer.class);
return total;
}
}
创建service层
package com.wang.service;
import com.wang.dao.UserDao;
import com.wang.pojo.User;
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void executeTest(){
userDao.executeTest();
}
public void updateInsert(User user){
userDao.updateInsert(user);
}
public void updateEdit(String id){
userDao.updateEdit(id);
}
public void updateDel(){
userDao.updateDel();
}
public int findNumbers(){
return userDao.findNumbers();
}
}
创建action包并在内创建UserAction类
package com.wang.action;
import com.wang.pojo.User;
import com.wang.service.UserService;
public class UserAction {
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void executesTest(){
userService.executeTest();
}
public void updateInsert(User user){
userService.updateInsert(user);
}
public void updateEdit(String id){
userService.updateEdit(id);
}
public void updateDel(){
userService.updateDel();
}
public int findNumbers(){
return userService.findNumbers();
}
}
创建applicationContext.xml并配置bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDao" class="com.wang.dao.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="userService" class="com.wang.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userAction" class="com.wang.action.UserAction">
<property name="userService" ref="userService"/>
</bean>
</beans>
创建测试类
package com.wang;
import com.wang.action.UserAction;
import com.wang.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public UserAction getController(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserAction userAction = context.getBean("userAction", UserAction.class);
return userAction;
}
public static void main(String[] args) {
Test test=new Test();
UserAction userAction=test.getController();
test.updateInsert(userAction);
test.testFindNumbers(userAction);
}
public void updateInsert(UserAction userAction){
User user=new User();
user.setId(7);
user.setName("张三");
user.setAge(20);
userAction.updateInsert(user);
}
public void testFindNumbers(UserAction userAction){
int numbers=userAction.findNumbers();
System.out.println("findNumbers方法的查询结果:");
System.out.println("共有"+numbers+"个用户");
}
}
运行结果为
遇到问题:无法加载applicationContext.xml
解决思路:首先查看applicationContext.xml单词有没有写错。如果没有就在pom.xml中加入以下代码
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
这是静态资源加载问题处理,加完后就可以运行了