My little goal

Made a project, maven+IDEA+MySQL

Because of frequent computer crashes and other reasons, I put it in my blog, share it, and record the process and problems I met. Whether I can grow up or not depends on the response of the caretaker.

Every place with a yellow background is the pit I met.

Title create project with IDEA

Let's first introduce the IDEA I use

Then, select Create from archetype, and the

The next one is easier. Name it and go all the way to next
There is a bug here, that is, after many people create projects, what seems to be missing in their own projects? For example, I don't have anything. The whole src is gone. Don't panic. I can create it by myself. The IDEA is also very powerful. You will be prompted at every step

Configure your pom.xml

Next are the documents pom.xml , which is mainly configured

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>

In short, it's package dependency. Let me introduce my own habits.

I like to refer to the previous successful project before each new project is built pom.xml Configuration, because it has been successful, there will be no error. Don't worry about the first time you use the package. IDEA is powerful enough to download for you. And if you can't download it, Magic conch , this link is used to download MySQL connector Java, version 5.1.38. Take one example and you can download other packages

Configure your web.xml

If not, create it yourself. Found or created in src\main\webapp\WEB-INF directory web.xml . What we need to add is this code

   <servlet-name>springmvc</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springmvc.xml</param-value>
   </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping> 

Configure your spring-mvc.xml

If it doesn't, create it by yourself, and it's not really a big probability. Create a new file in the SRC \ main \ resources directory springmvc.xml Documents. There are many functions that can be added here

    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/ssm" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>

This involves Configuration of datasourse in spring Problem, take the code I pasted as an example. The "ssm" in the first line is the name of the database. The second line is the code specially used in MySQL. The third and fourth lines are the account password needed to connect to the MySQL database. As for the table name, you can set it in other places

Configure your entity class

If not, create it yourself, and this one really doesn't. Under src/java, create a new package, com.xxx.entity , it is suggested that you keep your imagination on XXX, and keep the names of COM, entity and the last three unchanged.

    private Integer id;
    private String name;
    private String country;

Here, you just need to match the column names and column attributes in the table corresponding to the file names one by one. For example, I pasted it, right-click it, select generate, then select the fourth and sixth, and generate get, set, tostring methods

Configure your dao

And the previous layer, if not, create it by yourself, and this one really does not. Create a new Dao interface. If you name it, it's usually XxxDao. The code is also very simple, if my function is simple.

public List<Student> selectXxx();

Configure your business tier service

And the previous layer, if not, create it by yourself, and this one really does not. Here, you can as like as two peas.

public List<Student> selectUser();

Before we finish, create a new impl package in this package, and create a new XxxServiceImpl implementation class in the package

    @Resource
    private StudentDao dao;
    public List<Xxx> selectXxx() {
        return dao.selectXxx();
    }

Configure your control layer controller

And the previous layer, if not, create it by yourself, and this one really does not. Create a new controller package, and create a new control class XxxController in the package

@Controller
public class XxxController {
    @Resource
    private XxxService xxxService;
    
    @RequestMapping("index")
    public String index(Model model){
        List<Xxx> xxx = xxxService.selectXxx();
        model.addAttribute("list",xxx);
        return "index.jsp";
    }

Write your mapping file

If not, create it yourself, and this one really doesn't. Create a new XML directory in the resources directory, and then create a new XML directory in the XML directory Xxx.xml file

<mapper namespace="com.xxx.dao.XxxDao">
    <select id="selectXxx" resultType="com.xxx.entity.Xxx">
        select * from xxx
    </select>
</mapper>

Write your homepage file, usually in jsp format

If not, create it yourself, and this one really doesn't. New in webapp directory index.jsp Page. Most people are familiar here

<table border="3">
    <tr>
        <td>full name</td>
        <td>country</td>
    </tr>
    <c:forEach var="list" items="${list}">
        <tr>
            <td>${list.name}</td>
            <td>${list.country}</td>
        </tr>
    </c:forEach>
</table>

Configure your tomcat

Why don't people who don't understand this step ask Magic conch What about it?

It is worth noting that you need to add an artifast to this deployment. If you can't find it under normal circumstances, ask Magic conch

Run your project

Needless to say, I believe your final interface must be like this.

Tags: xml MySQL Spring JSP

Posted on Fri, 26 Jun 2020 02:24:51 -0400 by rharden