Build a Web server in 3 minutes -- Java Spring Boot + MySql + Android App

Keywords: Java MySQL Spring Tomcat server

Personal Taobao store link Partners in need can click here

1 Preparation

(1) Download the official website of tomcat, unzip it to / Library, start the Tomcat server, and the official website of Tomcat appears;
(2) When downloading MySql, the Mac should not download the latest version, because MySql cannot be opened in the system preferences. This is a pit. You can download the old version
(3) Download Navicat; This depends on personal choice. Database visualization is also available in IDEA
(4) IntelliJ IDEA, do not download the community version. Without the Java Web development environment, you can only download the professional version. You can crack it yourself or find a license from the Internet

2 project creation

IntelliJ Idea — File – new Project


You'd better choose Maven and click next

Select Spring Web and some MySql configurations, and click Finish

After creating a new Web project, you can view the pom.xml file. If it is the first time to create a project, it may not be highlighted. Click synchronize

2.1 project configuration

In the application.properties file, you can configure the port number of the server. If the 8080 port number is occupied, you can configure a port number yourself

//tomcat port number
server.port=8081

spring.datasource.url=jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=xxxxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2.2 MySql configuration


There is a DataBase selection box on the right. Click to select local MySql

Click Test Connection. You need to enter the user name and password. The connection can be successful only if the MySql service is enabled

3. Interact with database through JDBC

3.1 Dao

public interface LoginDao {

    List<UserInfo> getAllUser();
    //
    void Insert(UserInfo userInfo);
}

Dao defines the behavior of operating the database, such as obtaining all users in the database or inserting users into the database

3.2 operation database

@Service
public class LoginServiceImpl implements LoginDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<UserInfo> getAllUser() {

        List<UserInfo> userInfoList = jdbcTemplate.query("select * from UserInfo", new UserRowWrapper());

//        List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from UserInfo");
        return userInfoList;
    }

    @Override
    public void Insert(UserInfo userInfo) {
        jdbcTemplate.update("insert into UserInfo(username,password) value (?,?)",userInfo.getUsername(),userInfo.getPassword());

    }

}

In Java Web, the operation database generally uses JdbcTemplate, which encapsulates the template of SQL statement. You can operate the database through SQL statement

3.3 exposed external interfaces

In Spring MVC, the RestController RequestMapping annotation can be used to expose the interface

@RestController
public class LoginController {
    @Autowired
    private LoginDao loginDao;

    @RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
    public String getUserInfo(){

        String s = JSONArray.toJSONString(loginDao.getAllUser());
        return s;
    }

    @RequestMapping(value = "/saveUser",method = RequestMethod.GET)
    public void saveUser(@RequestParam(value = "username" )String username,@RequestParam(value = "password")String password){

        UserInfo userInfo = new UserInfo(username,password);

        loginDao.Insert(userInfo);
    }
}

After starting the service, enter http://localhost:8081/getUserInfo , you can get the data in the database

In fact, this implementation method is similar to the Room operation SQLite in Java. At present, a simple local server can be built through the above methods

Posted by LiveFree on Sat, 04 Dec 2021 22:14:02 -0800