wuheng 2 жил өмнө
parent
commit
fb681ff079

+ 22 - 0
day04/src/main/java/com/lovecoding/mvc/AnnoApplicataion.java

@@ -0,0 +1,22 @@
+package com.lovecoding.mvc;
+
+import com.lovecoding.mvc.config.SpringConfig;
+import com.lovecoding.mvc.config.SpringMvcConfig;
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+public class AnnoApplicataion extends AbstractAnnotationConfigDispatcherServletInitializer {
+    @Override
+    protected Class<?>[] getRootConfigClasses() {
+        return new Class[]{SpringConfig.class};
+    }
+
+    @Override
+    protected Class<?>[] getServletConfigClasses() {
+        return new Class[]{SpringMvcConfig.class};
+    }
+
+    @Override
+    protected String[] getServletMappings() {
+        return new String[]{"/"};
+    }
+}

+ 33 - 21
day04/src/main/java/com/lovecoding/mvc/Application.java

@@ -1,6 +1,10 @@
 package com.lovecoding.mvc;
 
+import com.lovecoding.mvc.config.SpringConfig;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.web.WebApplicationInitializer;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
 import org.springframework.web.context.support.XmlWebApplicationContext;
 import org.springframework.web.servlet.DispatcherServlet;
 
@@ -8,24 +12,32 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRegistration;
 
-public class Application implements WebApplicationInitializer {
-
-    @Override
-    public void onStartup(ServletContext servletContext) throws ServletException {
-        System.out.println( "我被调用了!!!" );
-
-        XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
-
-        applicationContext.setConfigLocation("classpath:spring.xml");
-
-        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
-
-        ServletRegistration.Dynamic dispatcher =
-                servletContext.addServlet("dispatcher", dispatcherServlet);
-
-        dispatcher.setLoadOnStartup(1);
-        dispatcher.addMapping("/");
-
-    }
-
-}
+//public class Application implements WebApplicationInitializer {
+//
+//    @Override
+//    public void onStartup(ServletContext servletContext) throws ServletException {
+//        //System.out.println( "我被调用了!!!" );
+//
+////        XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
+////        applicationContext.setConfigLocation("classpath:spring.xml");
+//
+//
+//
+//        AnnotationConfigWebApplicationContext applicationContext =
+//                new AnnotationConfigWebApplicationContext();
+//        applicationContext.register( SpringConfig.class );
+//
+//        //配置 MVC 配置档
+//        //配置 Mybatis配置档
+//
+//        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
+//
+//        ServletRegistration.Dynamic dispatcher =
+//                servletContext.addServlet("dispatcher", dispatcherServlet);
+//
+//        dispatcher.setLoadOnStartup(1);
+//        dispatcher.addMapping("/");
+//
+//    }
+//
+//}

+ 29 - 0
day04/src/main/java/com/lovecoding/mvc/MybatisTestController.java

@@ -0,0 +1,29 @@
+package com.lovecoding.mvc;
+
+import com.lovecoding.mvc.doman.Book;
+import com.lovecoding.mvc.mapper.BookMapper;
+import com.lovecoding.mvc.service.BookService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.List;
+
+@Controller
+public class MybatisTestController {
+
+    @Autowired
+    BookService bookService;
+
+    @GetMapping("/mybatis")
+    @ResponseBody
+    public String test(){
+
+        Book book = bookService.get(1L);
+        System.out.println( book );
+        return "OK";
+    }
+
+}

+ 31 - 0
day04/src/main/java/com/lovecoding/mvc/config/JdbcConfig.java

@@ -0,0 +1,31 @@
+package com.lovecoding.mvc.config;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+
+import javax.sql.DataSource;
+
+public class JdbcConfig {
+
+
+    @Value("${db.url}")
+    private String url;
+    @Value("${db.username}")
+    private String username;
+    @Value("${db.passwd}")
+    private String password;
+    @Value("${db.driver}")
+    private String driver;
+
+    @Bean
+    public DataSource getDataSource(){
+        DruidDataSource druidDataSource = new DruidDataSource();
+        druidDataSource.setUrl(url);
+        druidDataSource.setUsername(username);
+        druidDataSource.setPassword(password);
+        druidDataSource.setDriverClassName(driver);
+        return druidDataSource;
+    }
+
+}

+ 32 - 0
day04/src/main/java/com/lovecoding/mvc/config/MybatisConfig.java

@@ -0,0 +1,32 @@
+package com.lovecoding.mvc.config;
+
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.mapper.ClassPathMapperScanner;
+import org.mybatis.spring.mapper.MapperFactoryBean;
+import org.mybatis.spring.mapper.MapperScannerConfigurer;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.PropertySource;
+
+import javax.sql.DataSource;
+
+public class MybatisConfig {
+
+    @Bean
+    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
+        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
+        sqlSessionFactoryBean.setDataSource(dataSource);
+        return sqlSessionFactoryBean;
+    }
+
+    @Bean
+    public MapperScannerConfigurer getClassPathMapperScanner(){
+        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
+        mapperScannerConfigurer.setBasePackage("com.lovecoding.mvc.mapper");
+        return  mapperScannerConfigurer;
+    }
+
+}

+ 14 - 0
day04/src/main/java/com/lovecoding/mvc/config/SpringConfig.java

@@ -0,0 +1,14 @@
+package com.lovecoding.mvc.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+
+@Configuration
+@Import( {JdbcConfig.class, MybatisConfig.class}  )
+@ComponentScan("com.lovecoding.mvc")
+@PropertySource("classpath:db.properties")
+public class SpringConfig {
+}

+ 11 - 0
day04/src/main/java/com/lovecoding/mvc/config/SpringMvcConfig.java

@@ -0,0 +1,11 @@
+package com.lovecoding.mvc.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+
+@Configuration
+@ComponentScan("com.lovecoding.mvc")
+@EnableWebMvc
+public class SpringMvcConfig {
+}

+ 12 - 0
day04/src/main/java/com/lovecoding/mvc/doman/Book.java

@@ -0,0 +1,12 @@
+package com.lovecoding.mvc.doman;
+
+import lombok.Data;
+import org.springframework.stereotype.Repository;
+
+@Data
+public class Book {
+    Long book_id;
+    String book_name;
+    Long price;
+    Long stock;
+}

+ 11 - 0
day04/src/main/java/com/lovecoding/mvc/mapper/BookMapper.java

@@ -0,0 +1,11 @@
+package com.lovecoding.mvc.mapper;
+
+import com.lovecoding.mvc.doman.Book;
+
+import java.util.List;
+
+public interface BookMapper {
+
+    Book get(Long bookId);
+
+}

+ 8 - 0
day04/src/main/java/com/lovecoding/mvc/service/BookService.java

@@ -0,0 +1,8 @@
+package com.lovecoding.mvc.service;
+
+import com.lovecoding.mvc.doman.Book;
+
+
+public interface BookService {
+    Book get(Long bookId);
+}

+ 24 - 0
day04/src/main/java/com/lovecoding/mvc/service/BookServiceImpl.java

@@ -0,0 +1,24 @@
+package com.lovecoding.mvc.service;
+
+import com.lovecoding.mvc.doman.Book;
+import com.lovecoding.mvc.mapper.BookMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class BookServiceImpl implements BookService {
+
+
+    @Resource
+    BookMapper bookMapper;
+
+
+    public Book get( Long bookId) {
+        return bookMapper.get(bookId);
+    }
+
+}

+ 10 - 0
day04/src/main/resources/com/lovecoding/mvc/mapper/BookMapper.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.lovecoding.mvc.mapper.BookMapper" >
+
+    <select id="get" resultType="com.lovecoding.mvc.doman.Book" >
+        SELECT * FROM t_book WHERE book_id = #{bookId}
+    </select>
+
+</mapper>

+ 4 - 0
day04/src/main/resources/db.properties

@@ -0,0 +1,4 @@
+db.url = jdbc:mysql://127.0.0.1:13306/test?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
+db.username = root
+db.driver = com.mysql.cj.jdbc.Driver
+db.passwd = 0JZBdtlYoiOepddh

+ 35 - 0
pom.xml

@@ -31,6 +31,13 @@
             <artifactId>spring-webmvc</artifactId>
             <version>5.1.9.RELEASE</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-jdbc</artifactId>
+            <version>5.1.9.RELEASE</version>
+        </dependency>
+
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>javax.servlet-api</artifactId>
@@ -46,6 +53,34 @@
             <artifactId>jstl</artifactId>
             <version>1.2</version>
         </dependency>
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis</artifactId>
+            <version>3.5.7</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis-spring</artifactId>
+            <version>2.0.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.20</version>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.25</version>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.2.6</version>
+        </dependency>
 
         <dependency>
             <groupId>commons-fileupload</groupId>