Spring Boot前后端更新数据库操作示例

本文通过实例展示了如何在Spring Boot项目中进行数据库操作,包括项目配置、实体类创建、前端Ajax请求与后端Controller、Service、Dao层的交互。详细介绍了Controller、Service及Dao层的代码实现。
摘要由CSDN通过智能技术生成

1、项目配置

代码目录:

目录

application.properties文件:

spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://XXXX.XXXX.XXXX.XXXX:3306/数据库名称?setUnicode=true&characterEncoding=utf8
spring.datasource.username=用户名
spring.datasource.password=密码

pom.xml文件:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

2、为了查询,首先需要建立一个实体类,对应数据库中的一个表,每个属性对应着表中的列名

/**
customer的实体类
**/
/**快速创建getter和setter方法:Alt+Ins**/
public class customer {
   
    private int customer_id;
    private String customer_name;
    private String customer_mobile;
    private String customer_email;
    private String customer_username;
    private String customer_password;
    private String customer_address;
    private int customer_age;
    private String customer_sex;
    private Date customer_date;



    public void setCustomer_name(String customer_name) {
   
        this.customer_name = customer_name;
    }

    public String getCustomer_mobile() {
   
        return customer_mobile;
    }

    public void setCustomer_mobile(String customer_mobile) {
   
        this.customer_mobile = customer_mobile;
    }

    public String 
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ``` @Configuration @AutoConfigureAfter(MyBatisConfig.class) public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("com.example.mapper"); return mapperScannerConfigurer; } } ``` 这是一个 Spring Boot Starter 中使用 MyBatis 扫描 mapper 文件的示例代码。 首先在配置类 MyBatisMapperScannerConfig 中声明了一个名为 mapperScannerConfigurer 的 bean。 然后使用 setSqlSessionFactoryBeanName 方法指定 MyBatis 的 SqlSessionFactory bean 的名称。 最后使用 setBasePackage 方法指定 mapper 文件所在的包。 其中 MyBatisConfig.class 为Mybatis配置类,需要配置数据源和SqlSessionFactory,具体可以参考Mybatis官方文档 ``` @Configuration @MapperScan("com.example.mapper") public class MyBatisConfig { @Autowired private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } } ``` 另外还可以使用 @MapperScan 注解来简化配置。 ``` 这种方式可以省略 MyBatisMapperScannerConfig 配置类,在MybatisConfig.class中配置即可 ### 回答2: 自定义Spring Boot Starter是为了方便在Spring Boot项目中集成MyBatis数据库操作。以下是一个代码示例: 首先,在你的自定义Starter项目中创建一个自动配置类,例如`MyBatisAutoConfiguration`。在该类中,你可以使用Spring Boot的自动配置特性来配置MyBatis的相关属性,例如数据库连接信息、Mapper扫描等。同时,你也可以定义一些默认的配置项,方便用户进行使用。 ```java @Configuration @EnableConfigurationProperties(MyBatisProperties.class) @ConditionalOnClass(SqlSessionFactory.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class) public class MyBatisAutoConfiguration { @Autowired private MyBatisProperties properties; @Autowired private ResourceLoader resourceLoader; @Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setMapperLocations(this.properties.resolveMapperLocations()); return factory.getObject(); } @Bean @ConditionalOnMissingBean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @ConditionalOnMissingBean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer scanner = new MapperScannerConfigurer(); scanner.setSqlSessionFactoryBeanName("sqlSessionFactory"); scanner.setBasePackage("your.mapper.package"); return scanner; } } ``` 然后,你需要定义一个配置类`MyBatisProperties`来接收用户的自定义配置,例如数据库连接信息、Mapper扫描路径等。用户在自己的Spring Boot项目中,可以通过配置文件来配置这些属性。 ```java @ConfigurationProperties(prefix = "mybatis") public class MyBatisProperties { private String[] mapperLocations; // getters and setters public Resource[] resolveMapperLocations() { // 根据 mapperLocations 配置的路径解析为 Resource 数组 return Arrays.stream(this.mapperLocations) .map(location -> this.resourceLoader.getResource(location)) .toArray(Resource[]::new); } } ``` 最后,在你的自定义Starter项目中创建一个`spring.factories`文件,并在该文件中添加自定义Starter的自动配置类。 ``` org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.MyBatisAutoConfiguration ``` 用户在自己的Spring Boot项目中,只需要引入你的自定义Starter依赖,然后在配置文件中配置相关属性,即可使用MyBatis进行数据库操作。 以上就是一个自定义Spring Boot Starter操作MyBatis数据库的代码示例。希望对你有帮助! ### 回答3: 自定义 Spring Boot Starter 可以简化在项目中使用 MyBatis 操作数据库的配置和依赖管理。下面是一个简单的代码示例,演示如何自定义 Spring Boot Starter 并使用它来操作 MyBatis 数据库。 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 添加数据库驱动,这里以 MySQL 为例 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` 然后,创建一个 Spring Boot Starter 模块,在该模块中进行自定义配置和依赖管理。创建一个类,例如 `MyBatisStarterAutoConfiguration`,在该类中配置数据源和 MyBatis 相关的 bean: ```java @Configuration @ConditionalOnClass({ DataSourceProperties.class, SqlSessionFactory.class }) @EnableConfigurationProperties(MyBatisStarterProperties.class) public class MyBatisStarterAutoConfiguration { private final MyBatisStarterProperties properties; public MyBatisStarterAutoConfiguration(MyBatisStarterProperties properties) { this.properties = properties; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } } ``` 接下来,创建一个 properties 类,用于配置 MyBatis 配置文件的路径,默认为 `classpath:mybatis-config.xml`: ```java @ConfigurationProperties(prefix = "mybatis") public class MyBatisStarterProperties { private String configLocation = "classpath:mybatis-config.xml"; public String getConfigLocation() { return configLocation; } public void setConfigLocation(String configLocation) { this.configLocation = configLocation; } } ``` 在资源文件目录中,配置 MyBatis 的配置文件 `mybatis-config.xml`: ```xml <configuration> <!-- 根据需要配置 MyBatis 的其他相关配置 --> </configuration> ``` 最后,在项目的入口类中引入自定义的 Starter: ```java @SpringBootApplication @Import({ MyBatisStarterAutoConfiguration.class }) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 以上就是一个简单的示例,通过自定义 Spring Boot Starter 来操作 MyBatis 数据库。使用自定义的 Starter,我们可以将数据库和 MyBatis 的配置集中在一个模块中,使得项目的配置更加清晰和简洁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值