<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>6.1.14version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>6.1.14version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>6.1.14version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.21version>
dependency>
<dependency>
<groupId>jakarta.annotationgroupId>
<artifactId>jakarta.annotation-apiartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>6.1.14version>
dependency>
<dependency>
<groupId>jakarta.servletgroupId>
<artifactId>jakarta.servlet-apiartifactId>
<version>6.0.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.17.2version>
dependency>
<dependency>
<groupId>org.hibernate.validatorgroupId>
<artifactId>hibernate-validatorartifactId>
<version>8.0.0.Finalversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.16version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.25version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>6.1.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>6.1.14version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>3.0.3version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.23version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.5.12version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.5.12version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.34version>
dependency>
dependencies>
此处采用全注解开发,用java类的方式进行配置,并按照三层架构,框架整合的方式将所有配置拆分成5个配置类。这5个配置类又分别用于两个IoC容器,一个专门负责处理web请求(web容器),另一个负责处理后端数据(root容器),前者作为后者的子容器,使得可以在处理前端请求的时候调用后端service层等的方法
package com.atli.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
/**
* @ClassName:WebMvcJavaConfig
* @Author: hasee
* @Date: 2024.12.26 17:10
* @Description:
*/
// 配置类
@Configuration
// 配置包扫描
@ComponentScan({"com.atli.controller","com.atli.exceptionhandler"})
// 自动添加handlerMapping,handlerAdapter
@EnableWebMvc
public class WebMvcJavaConfig implements WebMvcConfigurer {
// 静态资源处理
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
// 视图解析器
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/", ".jsp");
}
// 拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
}
package com.atli.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @ClassName:ServiceJavaConfig
* @Author: hasee
* @Date: 2024.12.26 17:25
* @Description:
*/
// 配置类
@Configuration
// 包扫描
@ComponentScan("com.atli.service")
// 开启aspectj注解支持
@EnableAspectJAutoProxy
// 开启事务注解支持
@EnableTransactionManagement
public class ServiceJavaConfig {
// 事务管理器实现
@Bean
public TransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
package com.atli.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.util.Properties;
/**
* @ClassName:MapperJavaConfig
* @Author: hasee
* @Date: 2024.12.26 17:35
* @Description:
*/
// 配置类
@Configuration
public class MapperJavaConfig {
// 利用mybatis提供的SqlSessionFactoryBean配置SqlSessionFactory
// 千万注意,是mybatis提供的,如果要使用mybatisplus则需要用其他的工厂对象MybatisSqlSessionFactoryBean
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 配置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 指定mybatis配置文件功能
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//开启驼峰映射
configuration.setMapUnderscoreToCamelCase(true);
// 开启logback
configuration.setLogImpl(Slf4jImpl.class);
// 开启resultMap自动映射
configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
// 启用configuration实例中配置
sqlSessionFactoryBean.setConfiguration(configuration);
// 设置别名
sqlSessionFactoryBean.setTypeAliasesPackage("com.atli.pojo");
// 添加pagehelper插件
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
pageInterceptor.setProperties(properties);
sqlSessionFactoryBean.addPlugins(pageInterceptor);
return sqlSessionFactoryBean;
}
// mapper代理对象
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// 设置mapper接口与xml文件所在共同包
mapperScannerConfigurer.setBasePackage("com.atli.mapper");
return mapperScannerConfigurer;
}
}
package com.atli.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
/**
* @ClassName:DataSouceJavaConfig
* @Author: hasee
* @Date: 2024.12.26 18:53
* @Description: 若将配置datasouce和mapper放一个配置类中会因为加载顺序问题使得数据库连接信息为空
*/
@Configuration
// 导入配置文件
@PropertySource("classpath:jdbc.properties")
public class DataSourceJavaConfig {
// 导入数据库连接信息
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driver}")
private String driver;
// 配置数据源
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(password);
return ds;
}
}
package com.atli.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* @ClassName:SpringIoCInit
* @Author: hasee
* @Date: 2024.12.26 19:16
* @Description:
*/
@Configuration
public class SpringIoCInit extends AbstractAnnotationConfigDispatcherServletInitializer {
// rootioc容器配置类
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ServiceJavaConfig.class, DataSourceJavaConfig.class, MapperJavaConfig.class};
}
// webioc容器配置类
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebMvcJavaConfig.class};
}
// dispatcherServlet拦截路径
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
package com.atli.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;
/**
* @TableName customer
*/
@TableName(value ="customer")
@Data
public class Customer {
private Integer id;
private String userName;
private Integer capital;
}
package com.atli.pojo;
import lombok.Data;
import java.util.List;
/**
* @ClassName:CustomerPageResut
* @Author: hasee
* @Date: 2024.12.29 14:48
* @Description:用于包装分页查询结果
*/
@Data
public class CustomerPageResult {
Integer currentPage;
Integer pageSize;
long totalRecords;
List<Customer> customers;
}
package com.atli.controller;
import com.atli.pojo.Customer;
import com.atli.pojo.CustomerPageResult;
import com.atli.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ClassName:CustomerController
* @Author: hasee
* @Date: 2024.12.26 20:24
* @Description:
*
* todo:api: ssm/
* get "/customer/{currentPage}/{pageSize}"
* selectAllCustomers() 查询所有信息并按照页码,页面大小返回
* get "/customer/{id}"
* selectCustomerById() 根据id查询单个用户并返回
* post "/customer"
* insertCustomer() 新增用户
* delete "/customer/{id}"
* deleteCustomer() 删除用户
* put "/customer"
* updateCustomer() 修改用户
*/
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
@GetMapping("/customer/{currentPage}/{pageSize}")
public CustomerPageResult selectAllCustomers(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
return customerService.selectAllaCustomer(currentPage,pageSize);
}
@GetMapping("/customer/{id}")
public Customer selectCustomerById(@PathVariable("id") Integer id) {
return customerService.selectCustomerById(id);
}
@PostMapping("/customer")
public int insertCustomer(@RequestBody Customer customer) {
return customerService.insertCustomer(customer);
}
@DeleteMapping("/customer/{id}")
public int deleteCustomer(@PathVariable("id") Integer id) {
return customerService.deleteCustomer(id);
}
@PutMapping("/customer")
public int updateCustomer(@RequestBody Customer customer) {
return customerService.updateCustomer(customer);
}
}
package com.atli.service;
import com.atli.pojo.Customer;
import com.atli.pojo.CustomerPageResult;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author hasee
* @description 针对表【customer】的数据库操作Service
* @createDate 2024-12-26 19:35:01
*/
public interface CustomerService extends IService<Customer> {
CustomerPageResult selectAllaCustomer(Integer currentPage, Integer pageSize);
Customer selectCustomerById(Integer id);
int insertCustomer(Customer customer);
int deleteCustomer(Integer id);
int updateCustomer(Customer customer);
}
package com.atli.service.impl;
import com.atli.pojo.CustomerPageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atli.pojo.Customer;
import com.atli.service.CustomerService;
import com.atli.mapper.CustomerMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author hasee
* @description 针对表【customer】的数据库操作Service实现
* @createDate 2024-12-26 19:35:01
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
implements CustomerService{
@Autowired
private CustomerMapper customerMapper;
@Override
public CustomerPageResult selectAllaCustomer(Integer currentPage, Integer pageSize) {
PageHelper.startPage(currentPage, pageSize);
// 全体结果集
List<Customer> all = customerMapper.selectAllCustomer();
// 封装成pageInfo对象
PageInfo<Customer> pageInfo = new PageInfo<>(all);
// 创建查询结果集
CustomerPageResult customerPageResult = new CustomerPageResult();
// 赋值查询结果
customerPageResult.setCustomers(pageInfo.getList());
// 赋值当前页码
customerPageResult.setCurrentPage(pageInfo.getPageNum());
// 赋值单页信息条数
customerPageResult.setPageSize(pageInfo.getPageSize());
// 赋值总计条数
customerPageResult.setTotalRecords(pageInfo.getTotal());
return customerPageResult;
}
@Override
public Customer selectCustomerById(Integer id) {
return customerMapper.selectCustomerById(id);
}
@Override
public int insertCustomer(Customer customer) {
return customerMapper.insertCustomer(customer);
}
@Override
public int deleteCustomer(Integer id) {
return customerMapper.deleteCustomer(id);
}
@Override
public int updateCustomer(Customer customer) {
return customerMapper.updateCustomer(customer);
}
}
package com.atli.mapper;
import com.atli.pojo.Customer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author hasee
* @description 针对表【customer】的数据库操作Mapper
* @createDate 2024-12-26 19:35:01
* @Entity com.atli.pojo.Customer
*/
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {
List<Customer> selectAllCustomer();
Customer selectCustomerById(Integer id);
int insertCustomer(Customer customer);
int deleteCustomer(Integer id);
int updateCustomer(Customer customer);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atli.mapper.CustomerMapper">
<resultMap id="BaseResultMap" type="com.atli.pojo.Customer">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="capital" column="capital" jdbcType="INTEGER"/>
resultMap>
<insert id="insertCustomer" >
insert into Customer(user_name,capital) values (#{userName},#{capital})
insert>
<update id="updateCustomer">
update customer set user_name=#{userName} , capital=#{capital} where id=#{id}
update>
<delete id="deleteCustomer">
delete from customer where id=#{id}
delete>
<select id="selectAllCustomer" resultType="com.atli.pojo.Customer">
select * from Customer
select>
<select id="selectCustomerById" resultType="com.atli.pojo.Customer">
select * from Customer where id=#{id}
select>
mapper>
安装axios,element-plus
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
// 跨域
server: {
proxy: {
'/ssm':{
target: 'http://127.0.0.1:8080',
}
}
},
})
新增顾客
Edit
Delete