【SpringBoot】入门笔记02_用户登录注册_thymeleaf代替jsp_Spring Data JPA添加实体对象到数据库_h2内存数据库

一、创建SpringBoot项目。

添加如下依赖:MySQL是为了后面改项目的数据库方便的

2、创建实体对象,User ,其中,@Entity对应数据库中的表 @ID 指明主键  @GeneratedValue(strategy = GenerationType.IDENTITY)指明其唯一性。其中,需要构造无参构造方法和有参构造方法。

@Entity
public class User {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String username;
	private String password;
	private String telephone;
	
	protected User() {
	}
	public User(Long id, String username, String password, String telephone) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.telephone = telephone;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	
	
}

3、创建Controller

@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;
	
	@RequestMapping("/index.do")
	public ModelAndView login() {
		return new ModelAndView("index.html");
	}
	
	@RequestMapping("/user/register.do")
	public ModelAndView register(User user) {
		
		System.out.println(user.getUsername() + "   "+user.getPassword()+ "  "+user.getTelephone());
		userRepository.save(user);
		return new ModelAndView("redirect:/index.do");
	}
	
	@RequestMapping("/user/login.do")
	public ModelAndView login(User user) {
		System.out.println(user.getUsername() + "  "+user.getPassword());
		User userLogin = userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
		if(userLogin == null) {
			return new ModelAndView("redirect:/index.do");
		}else {
			return new ModelAndView("/welcome.html");
		}
		
	}
	
}

4、Repository 层 ,继承了CrudRepository<S,ID>方法,S对应处理的实体对象,ID对应主键的类型。

其中,CrudRepository有以下这些方法可使用,sava        
        savaAll
        findById
        existsById
        findAll
        findAllById
        count
        deleteById
        delete
        deleteAll

以下的findByUsernameAndPassword是自己添加的方法、并且必须遵循SpringDataJPA命名规则否则会报错

public interface UserRepository extends CrudRepository<User, Long> {
	//必须遵循Spring Data JPA规则命名
	User findByUsernameAndPassword(String username,String password);
}

5、自定义配置

#thymeleaf 编码格式
spring.thymeleaf.encoding=UTF-8
#热部署thymeleaf 
spring.thymeleaf.cache=false
#使用HTML5标准
spring.thymeleaf.mode=HTML5

#使用h2控制台
spring.h2.console.enabled=true

6、前端页面在此就不展示了。

7、查看H2控制台,其中JDBC URL:必须为jdbc:h2:mem:testdb

成功进入H2控制台,可以在此查看此时暂存的数据,H2作为内存式数据库时如果服务器关闭,那么数据就会消失。保存数据需要配置保存路径。 

8、Run as -> java application ,成功看结果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多啦CCCC梦

你的鼓励将是我最大的创作动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值