mirror of
https://github.com/Estom/notes.git
synced 2026-04-23 18:11:47 +08:00
spring 完结
This commit is contained in:
9
Spring/Spring5/code/shangguigu06/src/Main.java
Normal file
9
Spring/Spring5/code/shangguigu06/src/Main.java
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version $Id: ${NAME}, v 0.1 ${YEAR}-${MONTH}-${DAY} ${TIME} yinkanglong Exp $
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
39
Spring/Spring5/code/shangguigu06/src/bean01.xml
Normal file
39
Spring/Spring5/code/shangguigu06/src/bean01.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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">
|
||||
|
||||
<!--开启组件扫描
|
||||
1. 如果扫描多个包,多个包之间使用逗号隔开
|
||||
2. 扫描包的上层目录-->
|
||||
<context:component-scan base-package="com.ykl"></context:component-scan>
|
||||
|
||||
<!--引入外部属性文件-->
|
||||
<!-- <context:property-path path="classpath:jdbc.properties"/>-->
|
||||
|
||||
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
|
||||
<!-- <property name="driverClassName" value="${prop.driverClass}"></property>-->
|
||||
<!-- <property name="url" value="${prop.url}"></property>-->
|
||||
<!-- <property name="username" value="${prop.username}"></property>-->
|
||||
<!-- <property name="password" value="${prop.password}"></property>-->
|
||||
<!-- </bean>-->
|
||||
|
||||
<!--引入数据库连接-->
|
||||
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
|
||||
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
|
||||
<property name="url" value="jdbc:mysql://localhost:3310/user"></property>
|
||||
<property name="username" value="root"></property>
|
||||
<property name="password" value="123456"></property>
|
||||
</bean>
|
||||
|
||||
<!--创建jdbctemplate对象-->
|
||||
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<!--注入参数-->
|
||||
<property name="dataSource" ref="dataSource"></property>
|
||||
</bean>
|
||||
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
import com.ykl.entity.User;
|
||||
|
||||
import java.awt.print.Book;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserDo, v 0.1 2022-10-09 12:03 yinkanglong Exp $
|
||||
*/
|
||||
public interface UserDao {
|
||||
public int addUser(User user);
|
||||
|
||||
public void updateUser(User user);
|
||||
|
||||
public void delete(String id);
|
||||
|
||||
int selectCount();
|
||||
|
||||
User findUserInfo(String id);
|
||||
|
||||
List<User> findAll();
|
||||
|
||||
void batchAdd(List<Object[]> batchArgs);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
import com.mysql.cj.result.Row;
|
||||
import com.ykl.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserDoImpl, v 0.1 2022-10-09 12:04 yinkanglong Exp $
|
||||
*/
|
||||
@Repository
|
||||
public class UserDaoImpl implements UserDao {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public int addUser(User user) {
|
||||
String sql = "insert into user values(?,?,?)";
|
||||
Object[] args = {user.getUserId(),user.getUsername(),user.getUserStatus()};
|
||||
|
||||
int update = jdbcTemplate.update(sql,args);
|
||||
System.out.println(update);
|
||||
return update;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUser(User user) {
|
||||
String sql = "update user set username=?,userstatus=? where userid=?";
|
||||
Object[] args = {user.getUsername(),user.getUserStatus(),user.getUserId()};
|
||||
|
||||
int update = jdbcTemplate.update(sql,args);
|
||||
System.out.println(update);
|
||||
return ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String id) {
|
||||
String sql = "delete from user where userid=?";
|
||||
Object[] args = {id,};
|
||||
|
||||
int update = jdbcTemplate.update(sql,args);
|
||||
System.out.println(update);
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询返回整数
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int selectCount(){
|
||||
String sql = "select count(*) from user";
|
||||
Integer count = jdbcTemplate.queryForObject(sql,Integer.class);
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询返回单个记录
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public User findUserInfo(String id){
|
||||
String sql = "select * from user where userid=?";
|
||||
User user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),id);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findAll() {
|
||||
String sql = "select * from user";
|
||||
List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void batchAdd(List<Object[]> batchArgs) {
|
||||
String sql = "insert into user values(?,?,?)";
|
||||
int [] ints = jdbcTemplate.batchUpdate(sql,batchArgs);
|
||||
System.out.println(Arrays.toString(ints));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.entity;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : User, v 0.1 2022-10-10 09:14 yinkanglong Exp $
|
||||
*/
|
||||
public class User {
|
||||
private String userId;
|
||||
private String username;
|
||||
private String userStatus;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>userId</tt>.
|
||||
*
|
||||
* @return property value of userId
|
||||
*/
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>counterType</tt>.
|
||||
*
|
||||
* @param userId value to be assigned to property userId
|
||||
*/
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>username</tt>.
|
||||
*
|
||||
* @return property value of username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>counterType</tt>.
|
||||
*
|
||||
* @param username value to be assigned to property username
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>userStatus</tt>.
|
||||
*
|
||||
* @return property value of userStatus
|
||||
*/
|
||||
public String getUserStatus() {
|
||||
return userStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>counterType</tt>.
|
||||
*
|
||||
* @param userStatus value to be assigned to property userStatus
|
||||
*/
|
||||
public void setUserStatus(String userStatus) {
|
||||
this.userStatus = userStatus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"userId='" + userId + '\'' +
|
||||
", username='" + username + '\'' +
|
||||
", userStatus='" + userStatus + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.service;
|
||||
|
||||
import com.ykl.dao.UserDao;
|
||||
import com.ykl.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.awt.print.Book;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserService, v 0.1 2022-10-10 09:05 yinkanglong Exp $
|
||||
*/
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
public void addUser(User user){
|
||||
userDao.addUser(user);
|
||||
}
|
||||
|
||||
public void updateUser(User user){
|
||||
userDao.updateUser(user);
|
||||
}
|
||||
|
||||
public void deleteUser(String id){
|
||||
userDao.delete(id);
|
||||
}
|
||||
|
||||
public int findCount(){
|
||||
int number = userDao.selectCount();
|
||||
return number;
|
||||
}
|
||||
public User findOne(String id){
|
||||
return userDao.findUserInfo(id);
|
||||
}
|
||||
|
||||
public List<User> findAll(){
|
||||
return userDao.findAll();
|
||||
}
|
||||
|
||||
|
||||
public void batchAdd(List<Object[]> batchArgs){
|
||||
userDao.batchAdd(batchArgs);
|
||||
}
|
||||
}
|
||||
105
Spring/Spring5/code/shangguigu06/src/com/ykl/test/Test01.java
Normal file
105
Spring/Spring5/code/shangguigu06/src/com/ykl/test/Test01.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.test;
|
||||
|
||||
import com.ykl.entity.User;
|
||||
import com.ykl.service.UserService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import sun.security.util.ArrayUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Test01, v 0.1 2022-10-08 16:09 yinkanglong Exp $
|
||||
*/
|
||||
public class Test01 {
|
||||
|
||||
|
||||
@Test
|
||||
public void testAdd(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
User user = new User();
|
||||
user.setUserId("123");
|
||||
user.setUsername("yinkanglong");
|
||||
user.setUserStatus("up");
|
||||
userService.addUser(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
User user = new User();
|
||||
user.setUserId("1");
|
||||
user.setUsername("ykl1");
|
||||
user.setUserStatus("up1");
|
||||
userService.addUser(user);
|
||||
|
||||
user.setUserStatus("down1");
|
||||
userService.updateUser(user);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
User user = new User();
|
||||
user.setUserId("2");
|
||||
user.setUsername("ykl2");
|
||||
user.setUserStatus("up2");
|
||||
userService.addUser(user);
|
||||
|
||||
userService.deleteUser(user.getUserId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindCount(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
int num = userService.findCount();
|
||||
System.out.println(num);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindOne(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
User user = userService.findOne("123");
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAll(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
List<User> users = userService.findAll();
|
||||
System.out.println(users);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBatchAdd(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
UserService userService = context.getBean("userService",UserService.class);
|
||||
|
||||
|
||||
List<Object[]> batchArgs = new ArrayList<>();
|
||||
|
||||
Object[] o1 = {"111","java","a"};
|
||||
Object[] o2 = {"222","c++","b"};
|
||||
Object[] o3 = {"333","mysql","c"};
|
||||
batchArgs.add(o1);
|
||||
batchArgs.add(o2);
|
||||
batchArgs.add(o3);
|
||||
|
||||
userService.batchAdd(batchArgs);
|
||||
}
|
||||
}
|
||||
4
Spring/Spring5/code/shangguigu06/src/jdbc.properties
Normal file
4
Spring/Spring5/code/shangguigu06/src/jdbc.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
prop.driverClass=com.mysql.jdbc.Driver
|
||||
prop.url=jdbc:mysql://localhost:3306/user
|
||||
prop.username=root
|
||||
prop.password=123456
|
||||
Reference in New Issue
Block a user