About the beginning of the contact Springboot

This non-teaching Bo, just for my learning record, look where appropriate reference, the environment is Centos eclipse Enterprise Edition, comes jee and maven, so down a springboot plug on the project to create the beginning of a little pit encountered, response the method is a problem that deleted files in the directory /root/.m2/ folder in this directory can also create a maven settings.xml file configuration Ali source, or create a project can wait six months. . . Follows

   <settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
    <mirrors>
      <mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
      </mirrors>
    </settings>

After entering the eclipse point windows and preferences, select maven, then the user setting, just the path to write up, apply and close, bin.

Next topic, after creating a good project, I understand that, all the java folder in the following demo, which is at the same level of Application.java.

The first contact is connected mybatis my mysql, create a folder entity under demo, I am here to create a new entity class is User,

package com.example.demo.entity;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Data
@Getter
@Setter
@Component
public class User {
private String name;
private String pass;
private String mac1;
private String mac2;
private String mac3;
public String getname()
{
    return this.name;
}
public String getpass()
{
    return this.pass;
}
public List<String> getmac()
{
    List<String> maclist=new ArrayList();
    maclist.add(mac1);
    maclist.add(mac2);
    maclist.add(mac3);
    return maclist;
    
}
}

Then application.properties in the resources directory, edit its content, the last two lines can not, annotation method I use, so do not configure the mapper xml

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/Program?user=zhurui&password=Zhurui@111
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

Then create a mapper next demo folder, following a new UserMapper.java, content :( annotation method used)

package com.cun.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;

import com.example.demo.entity.User;

import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
//1、查询数据
    @Select("select * from user where name=#{name}")
    User getUser(String username) throws Exception;
}

Then create a folder in the service demo, create a corresponding interface class UserService.java

package com.example.demo.service;
import org.springframework.stereotype.Service;
import com.example.demo.entity.User;
//@Service
public interface UserService {
public User getUser(String name)throws Exception;
}

Then is the interface corresponding implementation classes, but also at creating ServiceImpl demo folder, create UserServiceImpl.java, content

package com.example.demo.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cun.mapper.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
@Service
public class UserSeviceImpl implements UserService{
@Autowired
private UserMapper mapper;
@Override
public User getUser(String name) throws Exception
{
    return mapper.getUser(name);
}
}

And, finally, create a controller to control the class folder in the demo, create Usercontroller.java

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;

@Controller
public class Usercontroller {
@Autowired
private UserService service;
@Autowired
private User user;
@RequestMapping(value="/login")
@ResponseBody
public User showlogin() throws Exception
{
    user=service.getUser("zhurui");
    System.out.print(user);
    return user;
}
}

This, modify the contents of Application.java

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.cun.mapper")
@SpringBootApplication
public class FirstProgramApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstProgramApplication.class, args);
    }

}

Also add a comment, oh yeah, finally succeeded, but I was a big crayons, intermediate configuration process pom slightly, so I put the content to digest, summary springboot working mode, and then continue to learn the .

Guess you like

Origin www.cnblogs.com/Wszhurui/p/11924352.html