idea搭建springboot+mybatis项目

file-new-project,新建一个spring initializr,在选择dependencies页面,web选项卡选择web,template engines页面选择thymeleaf,SQL选项卡选择 MySql、JDBC、MyBatis


创建好的结构目录


在springboot的配置文件中配置数据库连接(我习惯使用yml文件)

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/user
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

在templates文件夹下新建一个index.html,同时新建好mybatis对应的目录文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
<span th:text="${user.name}"></span>
</html>
@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(Model model){
        User user = userService.findUserByUserid(1);
        model.addAttribute("user",user);
        return "index";
    }
}
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public User findUserByUserid(Integer id) {
		User userByUserid = userMapper.findUserByUserid(id);
		return userByUserid;
	}
}
public interface UserMapper {

	@Select("select * from user where id = #{userId}")
	User findUserByUserid(@Param("userId") Integer userId);
}

最后在springboot的启动类上添加mapper文件的扫描

@SpringBootApplication
@MapperScan("cn.test.mapper")
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}
启动项目,访问http://127.0.0.1:8080/,即可






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值