白山云科技java_SpringBoot——04SSM

一.导入jar包

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

mysql

mysql-connector-java

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.1.1

com.github.pagehelper

pagehelper

4.1.0

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 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 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 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)

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

//获取自增Id

insert into t_user(name) values(#{name})

SELECT * FROM t_user

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

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值