首页 java开发 SpringBoot+SpringCloud+vue+Element开发项目—集成MyBatis框架

SpringBoot+SpringCloud+vue+Element开发项目—集成MyBatis框架

[2019-10-27 20:29 类别:java开发 阅读(203) ]

添加mybatis-spring-boot-starter依赖

pox.xml

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

添加mybatis配置类

新建config包  MybatisConfig.java

@Configuration
@MapperScan("com.read.mdh.*.dao")  //扫描dao包
public class MybatisConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.read.mdh.*.model"); //扫描model
        PathMatchingResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }
}

添加数据源配置

application.yml

spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mdh?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
    username: root
    password: root


修改启动类

MdhApplication.java

@SpringBootApplication(scanBasePackages = {"com.read.mdh"})
@EnableSwagger2
public class MdhApplication {

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

生产Mybatis模板 

http://www.mybatis.org/generator/index.html

https://blog.csdn.net/testcs_dn/article/details/77881776

http://mp.baomidou.com/#/

http://mp.baomidou.com/#/quick-start


Mapper中添加findAll方法

SysUserMappper.java

@Mapper
public interface SysUserMapper {
    int deleteByPrimaryKey(Long id);

    int insert(SysUser record);

    int insertSelective(SysUser record);

    SysUser selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(SysUser record);

    int updateByPrimaryKey(SysUser record);
    
    /**
     * 查询全部
     * @return
     */
    List<SysUser> findAll();
}


映射文件添加查询方法

SysUserMapper.xml

<select id="findAll" resultMap="BaseResultMap">
  select 
  <include refid="Base_Column_List" />
  from sys_user
</select>

service添加findAll方法

SysUserService.java

public interface SysUserService {

    /**
     * 查找所有用户
     * @return
     */
    List<SysUser> findAll();

}

service添加实现类调用SysUserMapper方法完成查询操作

SysUserServiceImpl.java

@Service
public class SysUserServiceImpl implements SysUserService {
    
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.findAll();
    }
}


编写用户管理RESTful接口,返回JSON数据格式,提供外部调用

SysUserController.java

@RestController
@RequestMapping("user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value ="findAll")
    public Object find(){
        return sysUserService.findAll();
    }
}


配置打包资源

修改pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <!--打包时复制mybatis的映射文件-->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/sqlmap/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>


关于博主Jansens