SpringBoot + MyBatis集成

Spring boot 持久化

1.创建Maven项目
2.到jar包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

<!-- mysql 数据库驱动. -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>	

<!-- spring-boot mybatis依赖: -->
	<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.1.1</version>
	</dependency>
<!-- MyBatis提供了拦截器接口  -->	
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>4.1.0</version>
</dependency>	
3.创建启动类
@SpringBootApplication
@MapperScan("cn.itsource.springboot.mybatis.mapper") //这里和以往不一样的地方就是MapperScan的注解,这个是会扫描该包下的接口
public class TestMyBatisXML {
    public static void main(String[] args) {
        SpringApplication.run(TestMyBatisXML.class);
    }
}
4.创建application.properties
########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
5.创建Domain层
public class Employee {
    private Long id;
    private String username;
    ......
6.创建Mapper层
public interface EmployeeMapper {
    @Select("select * from employee")
    public List<Employee> queryAll();
}
7.MyBatis映射
1.注解方式
public interface UserMapper {

    @Select("select * from user")
    public List<User> queryAll();
}
2.xml方式
1)EmployeeMapper.xml
<?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="cn.itsource.springboot.mybatis.mapper.EmployeeMapper">
    <!--void save(User user);-->
    <insert id="save" parameterType="Employee" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into employee(username) values(#{username})
    </insert>
</mapper>
2)配置别名(在application.properties中配置)

在这里插入图片描述

8.创建service层
@Service
@Transactional(propagation = Propagation.SUPPORTS) //开启事务,springboot实现了事务,只需要配置注解
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<Employee> queryAll() {
        return employeeMapper.queryAll();
    }
}
9.创建测试类
@ RunWith(SpringRunner.class)
@SpringBootTest(classes = TestMyBatisXML.class)
 public class TestssmXml {
       @Autowired
       private EmployeeMapper employeeMapper;
       @Test
       public void test(){
            for (Employee employee : employeeMapper.queryAll()) {
                System.out.println(employee);
           }
       }
    }

使用PageHelper分页

PageHelper是mybatis一个插件,可以用它完成分页

1.到jar包(上面已经导入了)
 <dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper</artifactId>
     <version>4.1.0</version>
 </dependency>
2.新建一个包config,在创建一个类MyBatisConfiguration
@Configuration //相当于我们建了applicationContext-xxx.xml <beans></beans>
public class MyBatisConfiguration {

    //相当于配置了一个bean
    //<bean class="com.github.pagehelper.PageHelper">
    //<property key="offsetAsPageNum"  value="true"></property>
    //<property key="rowBoundsWithCount"  value="true"></property>
    //<property key="reasonable"  value="true"></property>
    // <bean/>
	@Bean
    public PageHelper pageHelper() {
		System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}
3.service层配置

在这里插入图片描述

4.测试

如果config包和启动类不在同一个目录下需要进行扫描

@ComponentScan(“config包的权限的名”)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值