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包的权限的名”)