上一节中讨论了如何搭建springboot的开发环境,这节咱们继续在上一届基础上搭建ssm框架进行项目的开发。简单回顾了以前搭建ssm框架的过程,建立项目->集成springmvc->集成spring->集成mybatis->调试.手动搭建过程仍是比较复杂的。不过建议初学者仍是一步步来。不过在开发过程当中,你们仍是喜欢快一些。
有了springboot以后,咱们将会从繁重的项目搭建工做脱离出来。只须要执行几个简单的命令便可完成项目的建立和框架的搭建。html
spring init -g=com.briup.apps -a=app02 -p=war -d=web,mysql,mybatis app02
建立项目的时候指定依赖web,mysql,mybatis。而后进入到该项目中安装依赖便可前端
依赖安装完毕以后会发现控制台报错!以下mysql
经分析,是没有指定数据源,也就是说springboot不清楚你到底要链接哪一个数据库(mysql?oracle?)
这时候咱们能够在application.properties中添加以下数据源配置git
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数据库,须要添加以下依赖和配置github
...... <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将不会再报错!web
经过eclipse打开项目。找到pom.xml,在依赖中添加以下代码以添加动态部署功能。这样咱们修改代码后就不用再重启项目了。tomcat将会在咱们保存代码以后自动重启。spring
至此,ssm框架已经搭建好!对,你没有听错。
那么咱们应该如何进行开发呢?sql
三层架构和多层架构是咱们在开发过程当中经常使用的架构方式,用于解耦。每部分代码完成特定功能。这里,mybatis主要应用在数据访问层,springmvc应用于视图层,为前端提供数据接口(固然也能够直接渲染前端页面)。spring贯穿整个框架中。数据库
建立实体类编程
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); }
在这一层,须要面向接口编程,实际上也是对于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); } }
这里演示了添加查询的功能。
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 "成功啦"; } }
测试查询全部用户
测试录入用户
<!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结构中。