本文共 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 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/