springmvc固然好用,但是配置极其麻烦,今天就springboot搭建java web工程作以下详解。
①新建启动类application
@SpringBootApplication @MapperScan(basePackages = "com.cckj.dao") public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
这里需要注意Application启动类的位置,它的位置决定了它开启的时候是否能扫描service,controller等,他主要扫描它所在的包目录及子目录;
②在resources目录下新建配置文件application.properties类
# EMBEDDED SERVER CONFIGURATION (ServerProperties) server.port=8080 server.session-timeout=1800 server.context-path= server.tomcat.max-threads=0 server.tomcat.uri-encoding=UTF-8 server.tomcat.basedir=target/tomcat # HTTP encoding (HttpEncodingProperties) spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true #datasource spring.datasource.url=jdbc:mysql://localhost:3306/ccoee?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver #mybatis mybatis.type-aliases-package=com.cckj.model mybatis.mapper-locations=classpath:mapper/*.xml #mapper mapper.mappers=tk.mybatis.mapper.common.Mapper mapper.not-empty=false mapper.identity=MYSQL #pagehelper pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql #logging #logging.level.root=INFO #logging.level.org.springframework.web=DEBUG logging.level.org.zhangguan=DEBUG
或者考虑使用application.yml格式
#port server: port: 8083 context-path: /yiwei #datasource spring: datasource: url: jdbc:mysql://120.24.74.77:3306/online_test?useUnicode=true&useSSL=false&autoReconnect=true username: yiwei password: yiwei-mysql-66 driverClassName: com.mysql.jdbc.Driver #mybatis mybatis: type-aliases-package: com.zx.model mapper-locations: classpath:mapper/*.xml #输出控制台日志 logging: level: com: zx: mapper: debug
根据自己的项目需求替换上述的配置即可。
③编辑pom.xml
xml version="1.0" encoding="UTF-8"?>xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 zxp zxp 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-starter-web mysql mysql-connector-java com.alibaba druid 1.0.10 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 tk.mybatis mapper-spring-boot-starter 1.0.0 com.github.pagehelper pagehelper-spring-boot-starter 1.0.0
第一步:
第二步:编辑BaseModel
public class BaseModel { @Id @GeneratedValue(generator = "JDBC") private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }该类主要补充数据表的公有字段;
第三步:实体类继承BaseModel
public class User extends BaseModel { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }第四步:调用
public ListgetAll(){ return userDao.selectAll(); }
第五步:springboot的测试用例
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) public class ServiceTest { @Autowired UserDao userDao; @Test public void test() { Listlist = userDao.selectAll(); for (User user : list) { Assert.assertNotNull(user); System.out.println(user); } } }
这样spingboot的测试用例就ok了;
效果图:
@RestController @CrossOrigin @RequestMapping("/user") public class UserController extends BaseController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired UserService userService; @GetMapping(value = "/getUser") public Map,Object> getUser(){ List list = userService.getUser(); Map ,Object> map = new HashMap , Object>(); map.put("userList",list); return map; } }
BaseController代码:
public class BaseController { protected static String SUCCESS_RESPONSE = "{}"; protected static final int pageSize = 10; public HttpServletRequest request; public HttpServletResponse response; public HttpSession session; public Long userId; @ModelAttribute public void setReqAndRes(HttpServletRequest request,HttpServletResponse response){ this.request = request; this.response = response; this.session = request.getSession(); } public <T> Map, Object> pageList(List<T> list, Integer pageNum){ PageInfo<T> pageIngo = new PageInfo<T>(list); Map , Object> map = new HashMap ,Object>(); map.put("pages", pageIngo.getPages()); map.put("total", pageIngo.getTotal()); map.put("pageNum", pageNum); map.put("pageSize", pageSize); map.put("list", list); return map; } public int getPageNum(Integer pageNum){ return pageNum == null ? 1 : pageNum; } public Long getUserId(){ User user = (User)session.getAttribute(Constants.SESSION_USER_KEY); return user.getId(); } }
只需要在controller类的上面配置一个注解
@CrossOrigin
然后在对应的请求方法上指明请求的方式
@GetMapping(value = "/getUser")或者postMapping;
最后,直接运行application类就可以启动工程了,避免了配置tomcat的烦琐。
好了springboot 的java web工程就搭建完成了!我是张星,欢迎加入博主技术交流群,群号:313145288