Spring boot as a quick start is a good choice, and now does not seem to see anyone write a spring boot + gradle + mybatis in IntelliJ under the introductory article that happens to do. NET's classmates asked, I think I can also write such an article.
The biggest problem of getting started Java is the tedious steps, do not know how to start, before I also suggested that one of my younger brother with BlueJ to learn Java, but this thing learned he is very confused, can not get a sense of accomplishment, do not know what interesting things, then we have to point to the formal bar, See how to build Your own Java Web site from scratch.
Software Installation
As an introductory version, of course we'll start with the software installation.
The software to be installed has
- Jdk:http://www.oracle.com/technetwork/java/javase/downloads/index.html
- IntelliJ idea:https://www.jetbrains.com/idea/
- gradle:https://gradle.org/install/
- Tomcat: Optional, this part of the web is full of articles, do not repeat.
JDK installation is simple, download the latest version, follow the prompts to install.
IntelliJ idea installation I think it is not necessary to say, if this software installation will not, then learn to program the road, it can be a long way to repair.
Gradle also needs to be installed, under Windows, can be installed with scoop.
To install scoop, open Windows PowerShell, enter
set-executionpolicy remotesigned -s cu
Then use this command to install scoop:
iex (new-object net.webclient).downloadstring(‘https://get.scoop.sh‘)
For details, refer to: http://scoop.sh/
Under Mac, you can use homebrew to install with just one command:
brew update && brew install gradle
Create a project
Open IntelliJ, note that this software needs to be registered, but now there is an online activation of the server, anyway, you can think of ways.
Click Create New Project
Select Spring INITIALIZR
Fill your project with a little information, group and artifact you fill in the blanks (the format is not correct, IntelliJ will prompt you), type select Gradle project,packaging Select the war, then click Next.
Under the Web Select Web,template under Select Freemarker,sql below Select MyBatis, select the dependency will appear on the far right, I chose these three:
Then click Next, the Confirmation screen will appear, click the Finish button directly.
In the next screen, you need to gradle the path, you can find it in this way.
Create a new file, called Build.gradle, to write the following content
task getHomeDir << { println gradle.gradleHomeDir}
And then run
gradle getHomeDir
Finally, use this command to get the path:
gradle getHomeDir
Such as:
The Gradle path was obtained and filled into IntelliJ idea.
The rest of the settings are the same as me, then click OK.
At this point, you will go to the next screen, which is what is displayed at the top left:
At this time, you need to do is to wait patiently, perhaps you also need a VPN to complete the download dependency, this is your first challenge, this is the network reason, I am very difficult to help you.
However, you can change the Build.gradle file to speed up the download of maven dependencies and change it to this:
This part of the download speed can be greatly improved, or have to thank the horse's richest man ah.
In short, after all the content is loaded, you will see this interface:
The picture you see may not be the same as mine, because I have opened some more windows, but this is the structure of the project.
Creation of a database
In your MySQL, add a new database, called Life_master Bar, and then add such a table:
create table life_master . users (id int (10) auto_increment primary key, name varchar ( 45) not null, password varchar (20) not null);
Add two more data:
INSERT INTO users VALUES (1,‘Tom‘,‘no_pass‘);INSERT INTO users VALUES (2,‘Jerry‘,‘no_pass‘);
Double-click Open Application.properties, add the following:
spring.datasource.url=jdbc:mysql://192.168.88.3:3306/life_masterspring.datasource.username=root[email protected]spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
The above content according to your actual situation changes, my MySQL puts in the virtual machine's 192.168.88.3 above, the user name and the password all fill in your actual situation.
Start coding
Well, are you starting to wonder if you've been through a lot of trouble and now you've got something out of it?
Don't worry, let's add some code in.
Right-click on the Com.onefew.springboot and select New->java Class
Then give him a name, such as HomeController, the core content:
@Controller@RequestMapping ("/")PublicClass HomeController {@Autowired Userdao Userdao;@GetMapping ("/")PublicStringIndex(Model model) {model.AddAttribute ("Name","World");Return"Home"; }@GetMapping ("/{id}")public String findbyid (model model, @PathVariable (value = "id" ) int ID) { User u = Userdao. findbyid (ID); model. addattribute ( "name", U.getname ()); return "Home";}}
New user entity, named user, content:
PublicClass User {Privateint id;private String name;private String password;PublicStringGetName() {return name; }Public void SetName(String name) {This.name = name; }public String GetPassword() { return password;} public void setpassword(String password) {This . Password = password; }}
New Userdao interface, content:
@MapperPublicInterface Userdao {@Select ("SELECT * from users where id = #{id}")@Results ({@Result (property ="id", column ="id"), @Result (property = "name", column = "name"), @Result (property = Span class= "St" > "password", column = " Password ")}) user findbyid ( @Param ( "id" int ID);
Under the Templates directory, right-click the new file HOME.FTL, which reads as follows:
<?xmlVersion= "1.0" encoding= "UTF-8"?><! DOCTYPEHTML PUBLIC "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><Html xmlns="Http://www.w3.org/1999/xhtml" xml:lang= "zh" lang= "zh" ><head> <title> My first spring Boot program </title></< Span class= "Hljs-name" >head><body> Hello ${name}</body>< Span class= "Hljs-tag" ></HTML>
Modify Build.gradle, strong dependencies below, increase
‘mysql‘, name: ‘mysql-connector-java‘, version: ‘6.0.6‘
Test run
Before testing, temporarily comment out this content in Build.gradle:
providedRuntime(‘org.springframework.boot:spring-boot-starter-tomcat‘)
In the upper right corner dot small arrow, click Edit Configurations
New Spring Boot,
If all goes well, you can click on the little green Arrow at the top right, and you'll be up and running.
Spring-related information is output in the window
This time, open the browser, output http://127.0.0.1:8080
Busy for a while, now is the time to witness miracles:
http://127.0.0.1:8080 See Hello World, this is the content within index of our homecontroller.
HTTP://127.0.0.1:8080/1 and HTTP://127.0.0.1:8080/2 are the names of the people who read to our MySQL store.
Here, the content of the code is basically over.
Tomcat deployment
The previous mention of Tomcat, but this is optional, if you have Tomcat installed, then how to debug in tomcat?
Click Run->editconfigurations in the menu, click the plus sign in the top left corner of the popup menu, then select Tomcat server->local to do the configuration as in:
Note the settings within the deployment tag
When you're done, you can debug from Tomcat.
Conclusion
This is an introductory article, with a lot of, for beginners still have some challenges, but familiar with it, all this is very logical.
The content of this article not only covers the basics of building a project, but also involves Freemarker and MyBatis, although only point to stop, but from here to expand, I believe can write a little sense of accomplishment of the program.
The source address in this article: https://github.com/syler/Fun/tree/master/demo-spring-boot-1few
This address: http://www.cnblogs.com/asis/p/spring-boot-freemarker-mybatis-for-beginner.html
https://1few.com/spring-boot-freemarker-mybatis-for-beginner/
Spring boot + gradle + mybatis