SpringBoot整合
整合Spring Data JPA
Spring Boot + Spring MVC + Spring Data JPA + EasyUI 框架组合实现部门列表查询
- 导入数据库表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL COMMENT '用户名',
`password` varchar(100) DEFAULT NULL COMMENT '密码',
`name` varchar(100) DEFAULT NULL COMMENT '姓名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123456', '李四');
INSERT INTO `user` VALUES ('3', 'wangwu', '123456', '王五');
INSERT INTO `user` VALUES ('4', 'zhangwei', '123456', '张伟');
INSERT INTO `user` VALUES ('5', 'lina', '123456', '李娜');
INSERT INTO `user` VALUES ('6', 'lilei', '123456', '李磊');
- 创建maven工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
- 实体类的创建
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String userName;
private String password;
private String name;
//添加 get 和set 方法
}
- 创建DAO接口
public interface UserDao extends JpaRepository<User, Long> {
}
- 业务逻辑接口
public interface UserService {
List<User> findAll();
}
- 创建业务逻辑实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
List<User> list = this.userDao.findAll();
return list;
}
}
- 创建Controller
@RestController
@RequestMapping("user")
public class UserControlelr {
@Autowired
private UserService userService;
@RequestMapping("list")
public List<User> queryUserAll() {
List<User> list = this.userService.findAll();
return list;
}
- 创建引导类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
整合MyBatis
- 加入依赖
<!-- SpringBoot的Mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
- 编写Mapper
@Mapper
public interface UserMapper {
@Select("select * from user where name like '%${value}%'")
public List<User> queryUserByName(String name);
}
- 编写Service和Controller
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
List<User> list = this.userDao.findAll();
return list;
}
@Override
public List<User> queryUserByName(String name) {
List<User> list = this.userMapper.queryUserByName(name);
return list;
}
}
@RestController
@RequestMapping("user")
public class UserControlelr {
@Autowired
private UserService userService;
@RequestMapping("list")
public List<User> queryUserAll() {
List<User> list = this.userService.findAll();
return list;
}
@RequestMapping("list/{name}")
public List<User> queryUserAll(@PathVariable String name) {
List<User> list = this.userService.queryUserByName(name);
return list;
}
}