springboot整合mybatis

springboot整合mybatis

pom.xml,我用的是聚合工程

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.baosight</groupId>
    <artifactId>springboot-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>spring-boot-mybatis</artifactId>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <com-alibaba-druid>1.0.29</com-alibaba-druid>       
    </properties>

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${com-alibaba-druid}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>

        </dependency>
    <!-- 热加载插件,保存代码后不需要手动重启项目 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

  </dependencies>
  <!--maven 编译插件,生成的jar包可以通过 java -jar ***.jar 就可以运行起来,无需打成war包 -->
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.baosight.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

父工程pom:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.baosight</groupId>
    <artifactId>springboot-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>springboot-parent</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <java.version>1.8</java.version>
        <dubbo-spring-boot>1.0.0</dubbo-spring-boot>

        <com-alibaba-druid>1.0.29</com-alibaba-druid>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-parent</artifactId>
                <version>1.5.13.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.baosight.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <modules>
        <module>springboot-hello</module>
        <module>spring-boot-mybatis</module>

    </modules>
</project>

application.properties配置

#数据源
spring.datasource.url=jdbc:mysql://10.25.25.145:3306/jeecg
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#连接池类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#连接池配置
spring.datasource.initialSize=5  
spring.datasource.minIdle=5  
spring.datasource.maxActive=20  
#Mapper.xml放置路径
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
#JavaBean路径
mybatis.type-aliases-package=com.baosight.entity

这里推荐一个非常好用的自动生成代码的mybatis插件Mybatis-CMEU
这里写图片描述
可以生成包括service和实现类,图像化界面操作,非常方便,但是jdk需要1.8以上才能用。

dao层接口

注意@Mapper注解不要忘记了

@Mapper
public interface TSUserDao{
    /**
     * 获得TSUser数据集合,可以通过辅助工具Assist进行条件查询,如果没有条件则传入null
     * @param assist
     * @return
     */
    List<TSUser> selectTSUser(Assist assist);
}
###service层接口

public interface TSUserService{

/**
 * 获得TSUser数据集合,可以通过辅助工具Assist进行条件查询,如果没有条件则传入null
 * @param assist
 * @return
 */
List<TSUser> selectTSUser(Assist assist);
}
###service实现类

@Service
public class TSUserServiceImpl implements TSUserService{
@Autowired
private TSUserDao tSUserDao;
public List selectTSUser(Assist assist){
return tSUserDao.selectTSUser(assist);
}
}

###控制层

@RestController
public class Contorller {

@Autowired
private TSUserService tSUserService;

@RequestMapping("/list")
public List home(){
    return tSUserService.selectTSUser(new Assist());
}

}
“`
启动项目,浏览器访问 localhost:8080/list

阅读更多
版权声明:原创文章,转载注明出处 https://blog.csdn.net/u010816545/article/details/80322052
个人分类: spring boot mybatis
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

springboot整合mybatis

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭