你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

智能生物序列平台后端SpringBoot代码编写

2021/12/6 19:31:21

使用Springboot的MVC架构进行编写:

其中数据库操作部分使用mybatis自动生成基础代码,然后通过自己手写sql补充对应的缺失操作代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="/Users/jiangyi/IdeaProjects/demo/src/main/resources/mysql-connector-java-8.0.27.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?serverTimezone=GMT%2B8" userId="root" password="xxxxxx">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!--        <table tableName="Sort_AOPEDF_protein_drug_score" domainObjectName="Sort_AOPEDF_protein_drug_score" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
<!--        <table tableName="Sort_DeepDR_disease_drug_score" domainObjectName="Sort_DeepDR_disease_drug_score" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
<!--        <table tableName="Sort_deepDTnet_protein_drug_score" domainObjectName="Sort_deepDTnet_protein_drug_score" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <table tableName="Sort_HetDR_disease_drug_score" domainObjectName="Sort_HetDR_disease_drug_score" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

 其中注释中的文字说明配置单中所要填写的配置信息。

Springboot在配置mybatis时需要加载的依赖文件:

如下方代码;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration> <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </build>

</project>

其中dependencies中的org.mybatis.generator是生成器的依赖,其配置文件在<plugin>中的config标签中配置。

在使用mybatis时需要的步骤:

1.在配置文件中,写好对需要自动生成表的名字类

        <table tableName="Sort_HetDR_disease_drug_score" domainObjectName="Sort_HetDR_disease_drug_score" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

然后通过maven的包管理,自动运行mybatis的脚本文件。然后就自动生成了对应的文件。

 

2. 其中对应的生成文件中的标签如<Select>等,对应的是对数据库表的具体操作。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.dao.Sort_AOPEDF_protein_drug_scoreMapper" >
  <resultMap id="BaseResultMap" type="com.example.demo.entity.Sort_AOPEDF_protein_drug_score" >
    <result column="C1" property="c1" jdbcType="INTEGER" />
    <result column="C2" property="c2" jdbcType="VARCHAR" />
    <result column="C3" property="c3" jdbcType="VARCHAR" />
    <result column="C4" property="c4" jdbcType="VARCHAR" />
    <result column="C5" property="c5" jdbcType="DOUBLE" />
  </resultMap>
  <insert id="insert" parameterType="com.example.demo.entity.Sort_AOPEDF_protein_drug_score" >
    insert into Sort_AOPEDF_protein_drug_score (C1, C2, C3, C4, 
      C5)
    values (#{c1,jdbcType=INTEGER}, #{c2,jdbcType=VARCHAR}, #{c3,jdbcType=VARCHAR}, #{c4,jdbcType=VARCHAR}, 
      #{c5,jdbcType=DOUBLE})
  </insert>
  <insert id="insertSelective" parameterType="com.example.demo.entity.Sort_AOPEDF_protein_drug_score" >
    insert into Sort_AOPEDF_protein_drug_score
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="c1 != null" >
        C1,
      </if>
      <if test="c2 != null" >
        C2,
      </if>
      <if test="c3 != null" >
        C3,
      </if>
      <if test="c4 != null" >
        C4,
      </if>
      <if test="c5 != null" >
        C5,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="c1 != null" >
        #{c1,jdbcType=INTEGER},
      </if>
      <if test="c2 != null" >
        #{c2,jdbcType=VARCHAR},
      </if>
      <if test="c3 != null" >
        #{c3,jdbcType=VARCHAR},
      </if>
      <if test="c4 != null" >
        #{c4,jdbcType=VARCHAR},
      </if>
      <if test="c5 != null" >
        #{c5,jdbcType=DOUBLE},
      </if>
    </trim>
  </insert>
  <select id="selectByProtein" resultMap="BaseResultMap" parameterType="java.lang.String">
    select *
    from Sort_AOPEDF_protein_drug_score
    where C2 = #{C2}
  </select>
  <select id="selectByKey" resultMap="BaseResultMap" parameterType="java.lang.String">
    select *
    from Sort_AOPEDF_protein_drug_score
    where C1 = #{C1}
  </select>
</mapper>

最终搭建起来的框架可以在MVC架构的加持下,可以比较适合多人的代码编写,代码比较规范。