Remember the use of springboot+mybatis+freemarker+bootstrap (2)

Second, the use of springboot+mybatis

     1. Springboot's annotations: @SpringBootApplication : Startup project: Integrate common annotations (@Configuration, @EnableAutoConfiguration, @ComponentScan)/package sweep function (only under the current same-level package)

                @EnableAutoConfiguration auto configuration

                @ComponentScan scans some components such as controller, service and can scan multiple packages. Scan multiple packages usage @ComponentScan(basePackages={"com.zty.controller","com.zty.service"})

                @EntityScan scans packages for entity classes

                @MapperScan scans packages of Mapper classes

                @Controller indicates that this is a controller component

                @Service indicates that this is a service component

                @Mapper indicates that this is a Mapper component in Mybatis

                @Autowired autoload

                @RequestMapping specifies the route, and the mapping parameter responsible for the URL to the specific function in the Controller has the value method (optional) Example: @RequestMapping(value="/login", method=RequestMethod.POST)

                The string returned by @Responsebody is parsed into Json format

                @RestController is a rest-style annotation that contains @Responsebody

      There are common annotations that come to mind above. For more annotations, please refer to the springboot official website

     2. springboot project structure

            

 

 

           Under the App package is the java startup program, which is the main method of project startup, where @EntityScan(basePackages={"com.zty.entity"}) can be omitted because it is already included in @ComponentScan, the code is as follows

            

 1 @ComponentScan(basePackages={"com.zty.controller","com.zty.service"})
 2 @EntityScan(basePackages={"com.zty.entity"})
 3 @MapperScan(basePackages={"com.zty.mapper"})
 4 @EnableAutoConfiguration
 5 @SpringBootApplication
 6 public class App {
 7     
 8     public static void main(String[] args) {
 9         SpringApplication.run(App.class, args); 
10     }
11 }

           The controller code example is as follows

1  @Controller
 2  public  class LoginController {
 3          @RequestMapping("/") // Set the access path to the root directory of the domain name 
4      public String defaultView() {
 5          return "login"; // Return the login.ftl template in the view 
6      }
 7 }

         The entity package is the entity class in the project, which is omitted here

         The mapper package is the mapper class in mybatis. The annotation method is used here. The code example is as follows (note that the attribute names in the entity class must be consistent with those in the database, otherwise mybatis cannot be automatically injected)

1 @Mapper
2 public interface LoginMapper {
3             @Select("select id,name,password,depart,permission from user where name = #{name}")
4             User findUserByName(@Param("name") String name);
5 }

        The service code example is as follows

1 import com.zty.entity.User;
2 
3 public interface LoginService {
4     User findUserByName(String name);
5 }

        serviceimpl is the specific implementation of service, the code example is as follows

 1 @Service
 2 public class LoginServiceImpl implements LoginService{
 3     @Autowired
 4         private LoginMapper loginmapper;
 5     @Override
 6     public User findUserByName(String name) {
 7         // TODO Auto-generated method stub
 8         return loginmapper.findUserByName(name);
 9     }
10 
11 }

      So far, a simple project backend has been completed. Let's talk about the storage directory of static resources. Springboot will automatically search for resources in these directories.

      src/main/resources/static stores pictures or static resources such as js, css, and html

      The template files of the project are stored in src/main/resources/templates' (the template I use is freemarker)

      src/main/resources/static/webjars This is an empty directory, through which you can access bootstrap resources (the reason why the empty directory is named webjars may be because the bootstrap jar package name is webjars)

 

                            Notes from Ben Mengxin, if there is any mistake, please correct me:-p

        

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066758&siteId=291194637