java大搜罗springbootmybatis

springboot -mybatis注解版 和mybatis逆

2018-12-28  本文已影响6人  rainbowz

sprinboot中使用mybatis注解的一些配置

springboot版本2.0.1 mybatis-spring-boot-starter版本1.3.2

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

  <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

springboot中 mybatis逆向工程

项目截图

图片.png
appilication.properties
server.port=8788
spring.thymeleaf.cache=false
mybatis.mapper-locations= classpath:mapper/*.xml

mybatis.type-aliases-package=com.demo.entity


com.demo.test.number=${random.int[10,20]}
com.demo.test.name=GuanYu
com.demo.test.title=TheThreeKingdoms
com.demo.test.desc=${com.demo.test.name}Merry christma《${com.demo.test.title}》


spring.ldap.embedded.ldif=ldap-server.ldif
spring.ldap.embedded.base-dn=dc=didispace,dc=com
application.yml
  configuration:
     mapUnderscoreToCamelCase: true
  typeAliasesPackage: com.demo.entity
  mapperLocations: classpath:mapper/*.xml
spring:
  datasource:
    username: root
    password: 514730
    url: jdbc:mysql://localhost:3306/springboot
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource

generator.properties
jdbc.driverLocation=D:\\workspace4\\mysql\\mysql-connector-java-5.1.47.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql:///springboot
jdbc.userId=root
jdbc.password=514730

generatorConfig.xml
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!--导入属性配置 -->
    <properties resource="generator.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置 -->
    <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">


        <!-- optional,旨在创建class时,对注释进行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true" />
        </commentGenerator>


        <!--jdbc的数据库连接 -->
        <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}">
        </jdbcConnection>



        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.demo.enity" targetProject="src/main/java">
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="true"/>

            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>

            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="true"/>



            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>


        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator targetPackage="com.demo.dao" targetProject="src/main/java" type="MIXEDMAPPER">
            <property name="enableSubPackages" value=""/>

        </javaClientGenerator>




        <table schema="" tableName="student" enableCountByExample="true" enableSelectByExample="true"
               enableDeleteByExample="true" enableUpdateByExample="true" selectByExampleQueryId="true" >

        </table>   <table schema="" tableName="people" enableCountByExample="true" enableSelectByExample="true"
               enableDeleteByExample="true" enableUpdateByExample="true" selectByExampleQueryId="true" >
        </table>
    </context>
</generatorConfiguration>
ldap-server.ldif
dn: dc=didispace,dc=com
objectClass: top
objectClass: domain

dn: ou=people,dc=didispace,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=ben,ou=people,dc=didispace,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: didi
sn: zhaiyongchao
uid: didi
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=
log4j.properties
log4j.rootLogger=DEBUG, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} -&#45;&#45; [%15.15t] %-40.40c{1.} : %m%n%ex
        </Property>
        <Property name="LOG_FILE_PATH">logs</Property>
    </Properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>
        <RollingFile name="RollingFile" fileName="${LOG_FILE_PATH}/spring-boot-log4j2-demo.log" filePattern="${LOG_FILE_PATH}/spring-boot-log4j2-demo-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Filters>
                <!-- 只记录ERROR级别日志信息,程序打印的其他信息不会被记录 -->
                <!-- 此level设置的日志级别,是过滤日志文件中打印出的日志信息,和Root的level有所区别 -->
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
                <!-- 每天创建一个日志文件 -->
                <TimeBasedTriggeringPolicy interval="1" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </appenders>
    <loggers>
        <root level="INFO">
            <appender-ref ref="RollingFile" />
            <appender-ref ref="Console" />
        </root>
    </loggers>
</configuration>

dao层

@Component
public interface StudentMapper {
}

service层

public interface StuService {
    Student selectByPrimaryKey(Integer id);
    List<Student> list();
}

service层实现类

@Service("stuService")
public class StuServiceimpl implements StuService{

    @Autowired
    StudentMapper studentMapper;

     public  Student selectByPrimaryKey(Integer id){
        return  studentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Student> list() {
        return studentMapper.list();
    }
}

持久化层


@RestController
@RequestMapping("zz")
public class StuController {

    @Autowired
    StuService stuService;

    @RequestMapping(value = "/api/stu/{id}",method = RequestMethod.GET)
    public Student showAll(@PathVariable("id") Integer id){
            return  stuService.selectByPrimaryKey(id);
    }
    @RequestMapping(value = "/api/stu",method = RequestMethod.GET)
    public  List<Student> showAllStu(){
        return stuService.list();
    }
}
启动类
@EnableAutoConfiguration
@SpringBootApplication
@MapperScan("com.demo.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

在浏览器中访问 http://localhost:8788/zz/api/stu
(我的端口8788)

图片.png
上一篇下一篇

猜你喜欢

热点阅读