1. 使用maven构建SpringBoot的名叫spring-boot-mybatis项目
2. pom.xml
4.0.0 com.bjbs spring-boot-mybatis0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent1.5.13.RELEASE 1.8 3.0.2.RELEASE 2.0.4 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleaforg.mybatis.spring.boot mybatis-spring-boot-starter1.1.1 mysql mysql-connector-java8.0.27 com.mchange c3p00.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 ListfindAllUser(); void saveUser(User user); User findUserById(Integer id); void updateUser(User user); void deleteUserById(Integer id); }
7. 新建UserMapper.xml
8. 新建UserService.java
package com.bjbs.service; import java.util.List; import com.bjbs.pojo.User; public interface UserService { ListfindAllUser(); 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 ListfindAllUser() { 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. 运行项目并使用浏览器访问
16. 添加一个ID为41的用户
17. 点击ID为40的更新用户链接, 跳转到更新用户页面
18. 修改用户信息
19. 点击确定按钮, 保存用户信息
20. 点击ID为39的删除用户链接, 删除用户成功
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)