spring boot (1) 配置mybatis (idea)
话不多说,直接上代码和截图:
第一步:(IDEA,其实就是引用了官网的初始化地址,使用maven构建)
第二步:
第三步:
Restful 和websocket这些包先不引用,用的时候在添加
第四步:
选择数据使用的包,mybatis,postgre,jdbc
第五步:
上面这些没啥意思,估计人人都会,配置在下面,这里使用了spring boot 提供的
mybatis-spring-boot-starter依赖,集成mybatis只需要声明参数和路径就可以了
第六步:
添加配置
mybatis.type-aliases-package=com.kgo.helpmanual.common.entity
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://localhost:5432/vendor?useUnicode=true&characterEncoding=utf8&autoReconnect=true&stringtype=unspecified
spring.datasource.username = postgres
spring.datasource.password = dream
第一行:实体类
package com.kgo.helpmanual.common.entity;
public class ArticleEntity {
private String id ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
第二行:mybatis配置文件路径
第三行:mapper文件路径
第四行:数据库驱动名
spring.datasource.driverClassName=org.postgresql.Driver
第五行:数据库连接地址
spring.datasource.url = jdbc:postgresql://localhost:5432/vendor?useUnicode=true&characterEncoding=utf8&autoReconnect=true&stringtype=unspecified
第六行:用户名
第七行:密码
第七步:声明Dao层包扫描
package com.kgo.helpmanual;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.kgo.helpmanual.common.dao")
public class HelpmanualApplication {
public static void main(String[] args) {
SpringApplication.run(HelpmanualApplication.class, args);
}
}
实体代码
package com.kgo.helpmanual.common.entity;
public class ArticleEntity {
private String id ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
dao代码
package com.kgo.helpmanual.common.dao;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ArticleDao {
int getArticle();
}
mybatis配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
mapper代码
<?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.kgo.helpmanual.common.dao.ArticleDao">
<select id="getArticle" parameterType="String" resultType="Integer">
select count(*)
from uum.authority a
</select>
</mapper>
controller 代码
package com.kgo.helpmanual.controller;
import com.kgo.helpmanual.common.dao.ArticleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class ArticleController {
@Resource
private ArticleDao articleDao;
@RequestMapping("/artitle")
public String getArticle(){
return "测试文章接口";
}
@RequestMapping("/artitle2")
public String getArticle2(){
return String.valueOf(articleDao.getArticle());
}
}
启动类代码
package com.kgo.helpmanual;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.kgo.helpmanual.common.dao")
public class HelpmanualApplication {
public static void main(String[] args) {
SpringApplication.run(HelpmanualApplication.class, args);
}
}
注: 以上仅仅是demo
工程源代码:https://download.csdn.net/download/qq_26462567/10748167