上一节中讨论了如何搭建springboot的开发环境,这节我们继续在上一届基础上搭建ssm框架进行项目的开发。简单回顾了之前搭建ssm框架的过程,创建项目->集成springmvc->集成spring->集成mybatis->调试.手动搭建过程还是比较复杂的。不过建议初学者还是一步步来。不过在开发过程中,大家还是喜欢快一些。
1.初始化项目 ssm
有了springboot之后,我们将会从繁重的项目搭建工作脱离出来。只需要执行几个简单的命令即可完成项目的创建和框架的搭建。
spring init -g=com.briup.apps -a=app02 -p=war -d=web,mysql,mybatis app02
创建项目的时候指定依赖web,mysql,mybatis。然后进入到该项目中安装依赖即可
依赖安装完毕之后会发现控制台报错!如下
经分析,是没有指定数据源,也就是说springboot不清楚你到底要连接哪个数据库(mysql?oracle?)
这时候我们可以在application.properties中添加如下数据源配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ums?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
如果你用的是oracle XE数据库,需要添加如下依赖和配置
......
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=test
spring.datasource.password=test
server.port=8081
重新执行一次 mvn install将不会再报错!
通过eclipse打开项目。找到pom.xml,在依赖中添加如下代码以添加动态部署功能。这样我们修改代码后就不用再重启项目了。tomcat将会在我们保存代码之后自动重启。
至此,ssm框架已经搭建好!对,你没有听错。
那么我们应该如何进行开发呢?
2.三层架构
三层架构和多层架构是我们在开发过程中常用的架构方式,用于解耦。每部分代码完成特定功能。这里,mybatis主要应用在数据访问层,springmvc应用于视图层,为前端提供数据接口(当然也可以直接渲染前端页面)。spring贯穿整个框架中。
2.1 数据访问层
创建实体类
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String gender;
private String birth;
public User() {
}
public User(Long id, String name, String gender, String birth) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.birth = birth;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", gender=" + gender + ", birth=" + birth + "]";
}
}
创建Mapper接口
mybatis是一个轻量级的orm框架。建议在使用之前熟读官方文档http://www.mybatis.org/mybati...。 后面章节将会仔细介绍。这里仅使用。
为了使系统能扫描到mapper,需要在DemoApplication添加注释,@MapperScan("com.briup.apps.app02.dao")
@SpringBootApplication
@MapperScan("com.briup.apps.app02.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
public interface UserMapper {
/**
* 查询所有
* */
@Select("select * from tbl_user")
List<User> findAll() throws Exception;
/**
* 通过id查询
* */
@Select("select * from tbl_user where id = #{id}")
User findById(long id) throws Exception;
/**
* 保存
* */
@Insert("insert into tbl_user values(null,#{name},#{gender},#{birth})")
void save(User user);
}
2.2 业务逻辑层
在这一层,需要面向接口编程,实际上也是对于springmvc的应用,在后面的文章中将逐步对每个技术细化,这里推荐搭建查看spring-mvc的官方文档进行学习https://docs.spring.io/spring...
IUserService
public interface IUserService {
List<User> findAll() throws Exception;
User findById(long id) throws Exception;
void save(User user) throws Exception;
}
UserServiceImpl
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() throws Exception {
return userMapper.findAll();
}
@Override
public User findById(long id) throws Exception {
return userMapper.findById(id);
}
@Override
public void save(User user) throws Exception {
userMapper.save(user);
}
}
2.3 业务逻辑层
这里演示了添加查询的功能。
UserController
@RestController
@RequestMapping(path= {"/users"})
public class UserController {
@Autowired
private IUserService userService;
@GetMapping(path= {"/findAll"})
@JsonRawValue
public List<User> findAll() {
List<User> users = new ArrayList<>();
try {
users = userService.findAll();
} catch (Exception e) {
e.printStackTrace();
}
return users;
}
@GetMapping(path= {"/findById"})
@JsonRawValue
public User findById(@RequestParam long id) {
User user = null;
try {
user = userService.findById(id);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
@PostMapping(path= {"/save"}, consumes= {"application/x-www-form-urlencoded"})
public String save(@ModelAttribute User user) {
try {
userService.save(user);
} catch (Exception e) {
e.printStackTrace();
}
return "成功啦";
}
}
3. 测试
测试查询所有用户
测试录入用户
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单</title>
</head>
<body>
<h2>用户添加</h2>
<hr>
<form action="http://localhost:8080/users/save" method="post">
name: <input type="text" name="name"> <br>
gender:
<label for="gender_male">
男<input id="gender_male" type="radio" value="男" name="gender" checked="">
</label>
<label for="gender_female">
女<input id="gender_female" type="radio" value="女" name="gender">
</label> <br>
birth:
<input type="text" name="birth"> <br>
<input type="submit" value="保存">
</form>
</body>
</html>
思考! 在实际开发中,对于每个实体类我们都需要提供最基础增删改查的业务逻辑和数据访问,如果针对每个实体都写这么一堆势必造成代码的冗余,那么如何简化封装代码呢?之前我们项目中用到ssh,我封装了baseDao和baseService。那么这种思想同样可以应用到现在的ssm结构中。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。