Spring Boot + MyBatis + MySQL 整合

pom依赖:

<dependency>
   <groupId>org.mybatis.spring.bootgroupId>
   <artifactId>mybatis-spring-boot-starterartifactId>
   <version>1.3.1version>
dependency>
<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
   <groupId>mysqlgroupId>
   <artifactId>mysql-connector-javaartifactId>
   <scope>runtimescope>
dependency>
properties文件配置:

server.port=8888
logging.path=logs
logging.level.root=debug


spring.datasource.url=jdbc:mysql://127.0.0.1:3306/huangliang?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.type-aliases-package=com.example.demo.domain
mybatis.mapper-locations=classpath:mybatis/*.xml

Application文件配置如下:
@SpringBootApplication
public class PaDataApplication {
    @Bean
    public SpringUtils springUtils() {
        return new SpringUtils();
    }

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

mybatis的xml文件放到根目录下面

classpath:mybatis/*.xml  这个路经中
xml文件中写SQL语句:
<mapper namespace="com.example.demo.repository.MovieRepository">
    <insert id="saveMovie" parameterType="Movie" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO hl_movie(name,href) VALUES(#{name},#{href})
    insert>

mapper>
namespace指向repository接口文件
@Mapper
public interface MovieRepository {
    void saveMovie(Movie movie);
}
 
  
 
  
实体:
public class Movie{

    private Long id;
    private String name;
    private String href;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }
}

git码云参考地址:

https://gitee.com/hlaxjz/Spring-Boot-MyBatisZhengHeDeZhuaQuWangYeShuJuCunChuDaoShuJuKuJianDanLieZi

你可能感兴趣的:(spring,boot)