一.导入jar包
<dependencies>
<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>
<!--数据库依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybaits依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
2.创建启动类App.java
@SpringBootApplication
@MapperScan("cn.itsource.springboot.employee.mapper")//扫描Mapper的路径
public class SSMApplication {
public static void main(String[] args) {
SpringApplication.run(SSMApplication.class);
}
}
3.在application.yml添加配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
driver-class-name: com.mysql.jdbc.Driver
password: root
username: root
mybatis:
type-aliases-package: cn.itsource.springboot.employee.domain,cn.itsource.springboot.employee.query
4.编写DemoMapper
public interface UserMapper {
void save(User user);
List<User> queryPage(UserQuery query);
}
5.编写DemoService
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)//类级别是只读事务
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
//@Transactional 后两个配置是默认值
@Override
public void add(User user) {
userMapper.save(user);
// int i = 1/0;
}
@Override
public List<User> queryPage(UserQuery query) {
PageHelper.startPage(1, 2); //limit (pageNum-1)*pageSize,pageSize
return userMapper.queryPage(query);
}
}
6.编写Test
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SSMApplication.class)
public class IUserServiceTest {
@Autowired
private IUserService userService;
@Test
public void add()
{
User ww = new User("ww");
System.out.println(ww);//没有id
userService.add(ww);
System.out.println(ww);//有id
}
@Test
public void testList() throws Exception{
//com.github.pagehelper.Page --->PageList(currentPage,pageSizs,totalRecords,totalpages,Data)
Page<User> list = (Page) userService.queryPage(new UserQuery());
System.out.println(list.getClass());
System.out.println("总记录数:"+list.getTotal());
System.out.println("总页数:"+list.getPages());
System.out.println("当前页"+list.getPageNum());
System.out.println("每页多少条:"+list.getPageSize());
for (User o : list) {
System.out.println(o);
}
}
}
xml版本的xml(获取自增Id)
<?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.mapper.UserMapper">
<!--void save(User user);-->
<insert id="save" parameterType="User" useGeneratedKeys="true" keyProperty="id" keyColumn="id">//获取自增Id
insert into t_user(name) values(#{name})
</insert>
<!--List<User> queryPage(UserQuery query);-->
<select id="queryPage" resultType="User" parameterType="UserQuery">
SELECT * FROM t_user
</select>
</mapper>
annotation版本(获取自增Id)
public interface UserMapper {
@Insert("insert into t_user(name) values(#{name})")
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//获取自增Id
void save(User user);
}
分页插件的配置
@Configuration //相当于我们建了applicationContext-xxx.xml <beans></beans>
public class MyBatisConfiguration {
//相当于配置了一个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;
}
}