SpringBoot入门 之mybatis

数据库连接池druid
整合mybatis
单元测试
自动生成代码
分页插件
使用通用Mapper实现CRUD

数据库连接池druid
1.添加依赖

mysql
mysql-connector-java
5.1.35
runtime


com.alibaba
druid
1.0.9


org.springframework.boot
spring-boot-starter-web

2.在属性文件中配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/tiger_springboot
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource

2.实例化Druid对象

@Configuration
public class DruidDBConfig {

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;

@Bean
public DataSource getDateSource(){
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriverClassName(driverClassName);
    druidDataSource.setUrl(url);
    druidDataSource.setUsername(username);
    druidDataSource.setPassword(password);
    return druidDataSource;
}

}

3.打开监控中心
@Configuration
public class DruidConfiguration {

@Bean
public ServletRegistrationBean druidRegist(){
    servletRegistrationBean = new ServletRegistrationBean();
    servletRegistrationBean.setServlet(new StatViewServlet());
    servletRegistrationBean.addUrlMappings("/druid/*");
    servletRegistrationBean.addInitParameter("loginUsername","admin");
    servletRegistrationBean.addInitParameter("loginPassword","admin");
    return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean fiterRegist(){
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(new WebStatFilter());
    filterRegistrationBean.addUrlPatterns("/*");
    filterRegistrationBean.addInitParameter("exclusions",".js,*.git,*jpg,*.css,*.ico,/druid/*");
    filterRegistrationBean.addInitParameter("profileEnable","true");
    return filterRegistrationBean;
}

}

整合mybatis

1.添加依赖

org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1


org.projectlombok
lombok

2.在属性文件中的添加配置
mybatis:
mapper-locations:

3创建一个实体类
@Data
public class User {
private int id;
private String name;
private int age;
}

3.编写Dao
@Mapper
public interface UserMapper {
void save(User user);
}

在resources/mapper中


insert into user(name,age) values(#{name},#{age});

测试
@Test
public void testAdd(){
User user = new User();
user.setName(“aaa”);
user.setAge(20);
userMapper.save(user);
}

单元测试
@Test
public void testAaa(){
//断言
String aaa=“111”;
System.out.println(aaa);
// Assert.assertEquals(aaa,“222”);
Assert.assertNotEquals(aaa,“111”);
}

自动生成代码
1.复制一个自动生成的配置文件
2.添加一个插件

org.mybatis.generator
mybatis-generator-maven-plugin
1.3.7

src\main\resources\generatorConfig.xml
true
true



genarator

generate

generate-resources




mysql
mysql-connector-java
5.1.35


分页插件
1.添加依赖

com.github.pagehelper
pagehelper-spring-boot-starter
1.2.10

2.属性文件配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql

3.代码实现
创建一个保存查询的结果对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult {
private long total;//总条数
private List rows; //查询的数据
}

Service层实现分页
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
/**
*
* @param pageNum 当前页
* @param pageSize 每页条数
* @return
*/
public PageResult getUserList(int pageNum,int pageSize){
PageHelper.startPage(pageNum,pageSize);
Page page = (Page)userMapper.selectByExample(null);
return new PageResult(page.getTotal(),page.getResult());
}
}

测试
@Test
public void testPage(){
PageResult pageResult = userService.getUserList(2,2);
System.out.println(pageResult.getTotal());
List users = pageResult.getRows();
for(User u:users){
System.out.println(u);
}
}

使用通用Mapper实现CRUD
1.添加依赖

tk.mybatis
mapper-spring-boot-starter
2.1.5

2.添加配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/tiger_springboot
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
3.创建表
4.写POJO
@Data
@Table(name = “user”)
public class User {
@Id
@KeySql(useGeneratedKeys = true)
private int id;
@Column
private String name;
@Column(name = “age”)
private int age;
}
5.写Mapper
@Mapper
public interface UserMapper extends Mapper {
}
6.测试
@Autowired
private UserMapper userMapper;
@Test
public void testAdd(){
User user = new User();
user.setName(“ccc”);
user.setAge(40);
userMapper.insert(user);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值