016

016,第1张

016

1. 使用maven构建SpringBoot的名叫spring-boot-mybatis项目

2. pom.xml 



	4.0.0
	com.bjbs
	spring-boot-mybatis
	0.0.1-SNAPSHOT

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.13.RELEASE
	

	
	
		1.8
		
		3.0.2.RELEASE
		2.0.4
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.1.1
		
		
		
			mysql
			mysql-connector-java
			8.0.27
		
		
		
			com.mchange
			c3p0
			0.9.2
		
	

3. 在src/main/resources下创建全局配置文件application.properties

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.25.138:3306/StudyMybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=lyw123456

spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource

4. 数据库表设计

5. 新建User.java 

package com.bjbs.pojo;

import java.io.Serializable;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private Integer id;
	private String name;
	private String sex;
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date birthday;
	private String address;

	public User() {
	}
	
	public User(String name, String sex, Date birthday, String address) {
		this.name = name;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
	}
	
	public User(Integer id, String name, String sex, Date birthday, String address) {
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
				+ "]";
	}

}

6. 新建UserMapper.java

package com.bjbs.mapper;

import java.util.List;
import com.bjbs.pojo.User;

public interface UserMapper {
	public List findAllUser();
	
	void saveUser(User user);
	
	User findUserById(Integer id);
	
	void updateUser(User user);
	
	void deleteUserById(Integer id);
}

7. 新建UserMapper.xml





	
		select * from user where id = #{value}
	
	
	
		update user set name=#{name}, sex = #{sex}, birthday = #{birthday}, address = #{address} where id=#{id}
	
	
	
		delete from user where id = #{value}
	
	

8. 新建UserService.java

package com.bjbs.service;

import java.util.List;
import com.bjbs.pojo.User;

public interface UserService {
	List findAllUser();
	
	void saveUser(User user);
	
	User findUserById(Integer id);
	
	void updateUser(User user);
	
	void deleteUserById(Integer id);
}

9. 新建UserServiceImpl.java

package com.bjbs.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bjbs.mapper.UserMapper;
import com.bjbs.pojo.User;
import com.bjbs.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public List findAllUser() {
		return userMapper.findAllUser();
	}

	@Override
	public void saveUser(User user) {
		userMapper.saveUser(user);
	}

	@Override
	public User findUserById(Integer id) {
		return userMapper.findUserById(id);
	}

	@Override
	public void updateUser(User user) {
		userMapper.updateUser(user);
	}

	@Override
	public void deleteUserById(Integer id) {
		userMapper.deleteUserById(id);
	}

}

10. 新建App.java

package com.bjbs;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@MapperScan("com.bjbs.mapper") //@MapperScan 用户扫描MyBatis的Mapper接口
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

11. 在src/main/resources/templates下, 新建allUser.html



	
		
		展示用户数据
		
			th {
				width: 100px;
			}
		
	
	
		
			
				用户ID
				用户姓名
				用户性别
				用户生日
				用户地址
				 *** 作
			
			
				
				
				
				
				
				
					更新用户
					删除用户
					添加用户
				
			
		
	

12. 在src/main/resources/templates下, 新建addUser.html



	
		
		添加用户
	
	
		
	

13. 在src/main/resources/templates下, 新建editUser.html



	
		
		更新用户信息
	
	
		
	

14. 运行项目并使用浏览器访问

15. 点击添加用户链接, 跳转到添加用户页面 

16. 添加一个ID为41的用户 

17. 点击ID为40的更新用户链接, 跳转到更新用户页面 

18. 修改用户信息

19. 点击确定按钮, 保存用户信息

20. 点击ID为39的删除用户链接, 删除用户成功 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5719547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存