Spring增删改查
导入jar包
建立结构
User
public class User {
private int id;
private String name;
private String password;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
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 add(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);
}
}
IUserDao
public interface IUserDao {
List<User> findAll();
void delete(int id);
void add(String username,String password);
void update(int id,String username,String password);
}
UserService
@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 delete(int id) {
userDao.delete(id);
}
@Override
public void add(String username, String password) {
userDao.add(username,password);
}
@Override
public void update(int id, String username, String password) {
userDao.update(id,username,password);
}
}
IUserService
public interface IUserService {
List<User> findAll();
void delete(int id);
void add(String username,String password);
void update(int id,String username,String password);
}
Test
public class Test {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
IUserService userService=(IUserService) context.getBean("userService");
// userService.findAll();
// User user= (User) context.getBean("user");
// System.out.println(user);
// IUserDao userDao=(IUserDao) context.getBean("userDao");
// List<User> users=userDao.findAll();
// userDao.delete(4);
// System.out.println(users);
// IUserService userService= (IUserService) context.getBean("userService");
// List<User> users1=userService.findAll();
// System.out.println(users1);
// userService.update(6,"aa","123");
// List<User> users2=userService.findAll();
// System.out.println(users2);
// IUserService userService= (IUserService) context.getBean("userService");
List<User> users=userService.findAll();
System.out.println(users);
userService.add("ccc","ccc");
List<User> users1=userService.findAll();
System.out.println(users1);
}
}
spring.xml
<bean id="userService" class="com.zhongruna.service.Impl.UserService"></bean>
<bean id="user" class="com.zhongruna.bean.User">
<property name="id" value="1"></property>
<property name="name" value="abc"></property>
<property name="password" value="123"></property>
</bean>
<context:component-scan base-package="com.zhongruna"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property value="jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8" name="url"/>
<property value="root" name="username"/>
<property value="123456" name="password"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>