博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己的springboot 2.0的基本原理知识点梳理
阅读量:4141 次
发布时间:2019-05-25

本文共 6736 字,大约阅读时间需要 22 分钟。

编辑器:idea

一、.纯手写springboot框架 -创建内置Tomcat容器
1.依赖的导入

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.tomcat.embed
tomcat-embed-core
8.5.29
org.apache.tomcat
tomcat-jasper
8.5.29

2.创建servlet的测试类

@WebServlet(name = "IndexServlet")public class IndexServlet extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        response.getWriter().print("this is index.....tomcat");    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doPost(request,response);    }}

3.java代码启动Tomcat

public class Test001 {    private static int PORT = 8080;    private static String Contex_Path = "/itmayiedu";    private static String SERVLET_NAME = "/indexServlet";    public static void main(String[] args) throws LifecycleException {         //创建Tomcat服务器        Tomcat tomcatserver = new Tomcat();        //设置端口号        tomcatserver.setPort(PORT);        //是否设置自动部署        tomcatserver.getHost().setAutoDeploy(false);        //创建上下文        StandardContext standardContext = new StandardContext();        standardContext.setPath(Contex_Path);        //监听上下文        standardContext.addLifecycleListener(new Tomcat.FixContextListener());        //Tomcat容器添加上下文        tomcatserver.getHost().addChild(standardContext);        //创建servlet        tomcatserver.addServlet(Contex_Path,SERVLET_NAME,new IndexServlet());        //添加servleturl映射        standardContext.addServletMapping("/index",SERVLET_NAME);//        standardContext.addServletMappingDecoded("/index",SERVLET_NAME);        tomcatserver.start();        System.out.print("启动成功");        //异步进行接收请求        tomcatserver.getServer().await();    }}

二、.纯手写springboot框架 -使用注解启动springmvc

1.跟配置

@Configuration@ComponentScan("com.itmayiedu")public class RootConfig {}

2.扫描

public class spittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    //加载跟配置信息    @Override    protected Class
[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } //springmvc 加载配置信息 @Override protected Class
[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } //springmvc 拦截url映射 拦截所有请求 @Override protected String[] getServletMappings() { return new String[]{"/"}; }}

3.springmvc配置

@Configuration@EnableWebMvc@ComponentScan(basePackages = {"com.itmayiedu.controller"})public class WebConfig extends WebMvcConfigurerAdapter {    //配置视图转换器    @Bean    public ViewResolver viewResolver(){        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();        viewResolver.setPrefix("WEB-INF/jsp");        viewResolver.setSuffix(".jsp");        //可以在jsp页面通过${}访问beans、        viewResolver.setExposeContextBeansAsAttributes(true);        return viewResolver;    }}

service和controller层跟以前一样

三、springboot整合mybatis

1.依赖的导入:

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
com.itmayiedu.MybatisApplication
repackage

2.application.properties文件

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&useAffectedRows=true&serverTimezone=GMT%2B8&useAffectedRows=truespring.datasource.username=rootspring.datasource.password=12345spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.逻辑结构

(1)mapper层

public interface UserMapper {    @Select("select * from test where name=#{name}")    User findByName(@Param("name")String name);    @Insert("insert into test(name,age) values(#{name},#{age})")    int insert(@Param("name")String name,@Param("age")Integer age);}

(2)service层

@Servicepublic class UserService{    @Autowired    private UserMapper userMapper;    public User findByName(String name) {        return userMapper.findByName(name);    }    @Transactional    public int insertUser(String name,Integer age) {        int result=userMapper.insert(name, age);        return result;    }}

(3)controller层

@RestControllerpublic class UserController {    @Autowired    private UserService userService;    @RequestMapping("/insertUser")    public Integer insertUser(String name,Integer age) {        return userService.insertUser(name, age);    }    @Value("${itmayiedu.httpurl}")    private String httpurl;    @Value("${itmayiedu.name}")    private String name;    @Value("${itmayiedu.student.money}")    private String money;    @RequestMapping("/index")    public String index() {        return httpurl+name+money;    }}

(4)启动层

@SpringBootApplication@MapperScan(basePackages="com.itmayiedu.mapper")public class MybatisApplication {    public static void main(String[] args) {        SpringApplication.run(MybatisApplication.class, args);    }}

转载地址:http://pfkti.baihongyu.com/

你可能感兴趣的文章
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>