SpringBoot实践-实现简单的 增删改查 @Autowired报错 的检查级别
需求:实现一个用户管理系统,对用户进行CRUD操作
前端:easyUI
后端:SpringBoot+ssm+通用Mapper+druid+mysql
接下来,我们来看看如何用SpringBoot来玩转以前的SSM,我们用到的数据表tb_user和实体类User如下:
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` int(11) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`created` date DEFAULT NULL,
`updated` date DEFAULT NULL,
`note` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tb_user` VALUES ('1', 'ly', '123456', '柳岩', '18', null, '2000-12-06', null, null, null);
User:
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
// 用户名
private String userName;
// 密码
private String password;
// 姓名
private String name;
// 年龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 创建时间
private Date created;
// 更新时间
private Date updated;
// 备注
private String note;
public Long getId() {
return id;
}
// getter 和 setter方法
// ...
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password="
+ password + ", name=" + name + ", age=" + age + ", sex=" + sex
+ ", birthday=" + birthday + ", created=" + created
+ ", updated=" + updated + ", note=" + note + "]";
}
}
3整合启动器
3.1.1 整合SpringMVC启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.1.2 整合JDBC和事务启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
3.1.3 整合mysql驱动包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3.1.4 整合druid启动器
<!-- druid启动器
druid1.1.6启动器有严重bug,直接导致项目启动失败
druid1.1.10没有该问题
-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
3.1.5 整合mybatis启动器
<!-- mybatis的启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
3.1.6 整合通用Mapper启动器
<!-- 通用mapper的启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
3.2 编写代码:查询所有用户信息
3.2.1 dao
@org.apache.ibatis.annotations.Mapper // mybatis
public interface UserMapper extends Mapper<User> {
}
3.2.2 controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
@ResponseBody// 结果返回json
public List<User> findAll(){
List<User> list = userService.findAll();
return list;
}
}
3.2.3 service
/**
* Service中实现业务
* 用户的CRUD操作,在此类中需要提供 增、删、改、查 4个方法
*/
@Service
@Transactional// 注解
public class UserService {
@Resource
private UserMapper userMapper;
public List<User> findAll() {
return userMapper.selectAll();
//userMapper.增删改查
}
}
报错:
原因:IDE自身检查级别的原因。
解决方案:需要调整IDE对于@Autowired的检查级别
解决步骤:
1 、File — Settings
2、需要从Error改为Warning
3.2.4 pojo
@Table(name = "tb_user")
public class User {
@Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)//对应mysql的自增长主键策略
private Integer id;
private String userName;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private String note;// 说明,备注
private Date created;// 创建时间
private Date updated;// 更新时间
}
3.3 properties属性文件
3.3.1 连接数据库四大金刚
# 连接四大金刚 等号前面是key,是固定写法,等号后面是根据自己的数据库来定义
spring.datasource.url=jdbc:mysql://localhost:3306/ssm_user
spring.datasource.username=root
spring.datasource.password=123456
# 可省略,SpringBoot自动推断 驱动
spring.datasource.driverClassName=com.mysql.jdbc.Driver
3.3.2 数据库连接池druid
# druid连接池
#初始化连接数
spring.datasource.druid.initial-size=1
#最小空闲连接
spring.datasource.druid.min-idle=1
#最大活动连接
spring.datasource.druid.max-active=20
#获取连接时测试是否可用
spring.datasource.druid.test-on-borrow=true
#监控页面启动
spring.datasource.druid.stat-view-servlet.allow=true
3.4 测试访问
完成测试