commons-logging-1.2.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mysql-connector-java-5.1.8.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.3.6.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar
bean,dao, service, servlet, test, util
package com.zhongruan.bean;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserDao {
List<User> findAll();
void delete(int id);
void insert(String username, String password);
void update(int id,String username,String password);
}
package com.zhongruan.dao.impl;
import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class UserDao implements IUserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> findAll() {
String sql="select * from tb_user";
BeanPropertyRowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
List<User> users=jdbcTemplate.query(sql,rowMapper);
return users;
}
@Override
public void delete(int id) {
String sql="delete from tb_user where id=?";
jdbcTemplate.update(sql,id);
}
@Override
public void insert( String username, String password) {
String sql="insert into tb_user(username,password) values(?,?)";
jdbcTemplate.update(sql,username,password);
}
@Override
public void update(int id, String username, String password) {
String sql="update tb_user set username=?,password=? where id=?";
jdbcTemplate.update(sql,username,password,id);
}
}
package com.zhongruan.service;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserService {
List<User> findAll();
void delete(int id);
void insert(String username,String password);
void update(int id,String username,String password);
}
package com.zhongruan.service.impl;
import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAll() {
List<User> users=userDao.findAll();
return users;
}
@Override
public void update(int id,String username,String password) {
userDao.update(id,username,password);
System.out.println("update");
}
@Override
public void insert(String username, String password) {
userDao.insert(username,password);
System.out.println("insert");
}
@Override
public void delete(int id) {
userDao.delete(id);
System.out.println("delete");
}
}
package com.zhongruan.test;
import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
IUserService userService=(IUserService) context.getBean("userService");
List<User>users=userService.findAll();
System.out.println(users);
userService.delete(2);
userService.insert("sisu","0416");
userService.update(3,"xlb","6");
}
}
<?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">
<!-- <bean id="userService" class="com.zhongruan.service.impl.UserService"></bean>-->
<bean id="user" class="com.zhongruan.bean.User">
<property name="id" value="1"></property>
<property name="username" value="sisu"></property>
<property name="password" value="123"></property>
</bean>
<context:component-scan base-package="com.zhongruan"></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://127.0.0.1:3306/zjgm?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>