终于开始自己写后台了,我现在算java全能了吧,android,java gui和java后端,这是我一个人就能开发一个产品的节奏了

idea:IntelliJ IDEA 2022.1.2 (Community Edition)

数据库:MySQL

依赖:mybatis+mysql+druid

SpringBoot工程创建就不说了

先在pom内引入依赖,版本去https://mvnrepository.com/

 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.11</version>
        </dependency>

在application.properties内配置数据库

# 192.168.1.32:3306/test改成自己的数据库,username和password也改成自己的
spring.datasource.url=jdbc:mysql://192.168.1.32:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

新建个包,包名自己随便取,比如我的包名叫mapper,再创建一个interface,加上@Mapper和@Select,写一个简单的查询数据库功能

@Mapper
public interface DBMapper {
    @Select("select v.video_name from video v")
    List<String> getVideoName();
}

再创建一个类,用来配置数据库和druid,将@MapperScan内的basePackages指向我刚才创建的包,然后关于druid的设置网上很多,而且我还不太会设置,会了再说

@Configuration
@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionFactoryRef = "dbTestSqlSessionFactory")
public class DBTestDataSourceConfig {
    @Bean(name="dbTestDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DruidDataSource dbTestDataSource() {
        return new DruidDataSource();
    }
    @Bean(name="dbTestSqlSessionFactory")
    @Primary
    public SqlSessionFactory dbTestSqlSessionFactory(@Qualifier("dbTestDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
    @Bean(name="dbTestTransactionManager")//配置事务
    @Primary
    public DataSourceTransactionManager dbTestTransactionManager(@Qualifier("dbTestDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name="dbTestSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate dbTestSqlSessionTemplate(@Qualifier("dbTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name="dbTestDruidServlet")
    @Primary
    public ServletRegistrationBean<Servlet> dbTestDruidServlet() {
        // 进行 druid 监控的配置处理
        ServletRegistrationBean<Servlet> srb = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/**");
        // 白名单
        srb.addInitParameter("allow", "");
        // 是否可以重置数据源
        srb.addInitParameter("resetEnable", "false");
        return srb;
    }

    @Bean(name="dbTestFilterRegistrationBean")
    @Primary
    public FilterRegistrationBean<Filter> dbTestFilterRegistrationBean() {
        FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
        frb.setFilter(new WebStatFilter());
        // 所有请求进行监控处理
        frb.addUrlPatterns("/*");
        // 排除名单
        frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
        return frb;
    }
}

写个简单的TestController

@RestController
public class TestController {
    @Autowired
    DBMapper dbMapper;

    @GetMapping("test")
    @Transactional(transactionManager = "dbTestTransactionManager")
    public List<String> test(){
        return dbMapper.getVideoName();
    }
}

Run一下,然后在网页上输入地址localhost:8080/test/test

 把公司数据库内的电视剧给取出来了

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐