导入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
编写application.yml文件;
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?characterEncoding=utf-8&serverTimezone=UTC
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
initialization-mode: always
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidCoofig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//配置一个管理后台的servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
Map<String,String> initParams=new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");
bean.setInitParameters(initParams);
return bean;
}
//配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean=new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams=new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setUrlPatterns(Arrays.asList("/*"));
bean.setInitParameters(initParams);
return bean;
}
}
这一步实现之后就可以使用浏览器测试成功没有,启动SpringBoot项目。然后在浏览器地址栏输入:
编写一个与数据库里面的表对应的一个实体类,也就是包含数据库里面的字段(就像下图红色矩形框)。
第五步:编写mapper接口:并将sql语句直接用注解的方式写在方法名上面。
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface EmpMapper {
//根据eid查询一个Emp对象
@Select("select * from emp where eid=#{eid}")
Emp getEmpById(int eid);
//增删改都可以设置为void,或者boolean类型的方法
//int 表示执行之后受影响的行数,boolean代表执行成功与否
//增加一个Emp记录,
@Insert("insert into emp values(#{eid},#{ename})")
int insertEmp(Emp emp);
//删除一个Emp记录
@Delete("delete from emp where eid=#{eid}")
int deleteEmp(int eid);
//更改一个Emp记录
@Update("update emp set ename=#{ename} where eid=#{eid}")
int updateEmp(Emp emp);
}
同时在SpringBoot启动类的上面写入@mapperscan:这里的value填入你mapper接口的全包名
也可以单独在mapper接口类上面写入@mapper注解即可,这两个添加mapper注解的方法二选一
第六步:编写controller进行测试
import com.haihai.selfwriting02.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
EmpMapper empMapper;
@RequestMapping("/select/{eid}")
public Emp select(@PathVariable("eid") int eid){
return empMapper.getEmpById(eid);
}
@RequestMapping("/insert")
public int insert(){
Emp emp=new Emp(1899,"1899");
return empMapper.insertEmp(emp);
}
@RequestMapping("/delete/{eid}")
public int delete(@PathVariable("eid") int eid){
return empMapper.deleteEmp(eid);
}
@RequestMapping("/update")
public int update(){
Emp emp=new Emp(155,"155");
return empMapper.updateEmp(emp);
}
}
注解版使用起来会比配置版简单,快速。在实现简单的sql语句时(就是简单的增删查改),比较有优势。整合配置文件版本的mybatis,过程会稍微复杂一些,但是在处理复杂sql语句,比如动态sql的时候(也就是可以根据传入的字段(可多可少)进行相应的查询或者其他操作),会比较有优势。
如果想要查看SpringBoot整合配置文件版的mybatis可以去下面的链接,下面是超链接,直接点击就可以了。
亲,点我,点我查看配置文件版
今天,在我写了一个月博客之后,终于收获了第一个粉丝,虽然我还不知道您是谁,也不知道,您是看了我的哪一篇博客才关注的我,但是,(重点来了哈),但是我非常非常的感谢您的支持,当我今天晚上看到我有一个粉丝的时候,我真的超级激动,炒鸡炒鸡鸡冻,我会努力继续加油的,再次感谢您的支持。