Home > Database > Mysql Tutorial > body text

mysql connects to the database and tests instance sharing

小云云
Release: 2018-02-23 14:43:07
Original
1929 people have browsed it

This article mainly shares with you mysql database connection and test examples, hoping to help everyone.

1. Import the jar package about mybatis through maven

mysql connects to the database and tests instance sharing

You can directly download the relevant jar package through Alibaba Cloud's maven library

<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
    </dependency>
<dependency>
    <groupId>MySQL</groupId>
    <artifactId>mysql-connector-Java</artifactId>
    <version>5.1.38</version>
</dependency>
</dependencies>
Copy after login

2. Write the configuration file of mybatis


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<environments default="development">  
    <environment id="development">  
        <transactionManager type="JDBC" />  
        <!-- 配置数据库 -->
        <dataSource type="POOLED">  
            <property name="driver" value="com.mysql.jdbc.Driver" />  
            <property name="url" value="jdbc:mysql://localhost:3306/my_schema?useSSL=true" />  
            <property name="username" value="root" />  
            <property name="password" value="" />  
        </dataSource>  
    </environment>  
</environments>  
<!-- 映射文件 -->
<mappers>  
    <mapper resource="UserMapper.xml" />  
</mappers>  
</configuration>
Copy after login

3. Write the mapping file UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
<mapper namespace="userMapper">  
<select id="selectUser" resultType="entity.User">  
    select * from user  
</select>  
</mapper>
Copy after login

4. Write the entity class User, which is a very common class, so I won’t explain too much.

public class User {
private String username;
private String email;
private String password;
private String create_time;
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getCreate_time() {
    return create_time;
}
public void setCreate_time(String create_time) {
    this.create_time = create_time;
}
@Override
public String toString() {
    return "User [username=" + username + ", email=" + email + ", password=" + password + ", create_time="
            + create_time + "]";
}
public User(String username, String email, String password, String create_time) {
    super();
    this.username = username;
    this.email = email;
    this.password = password;
    this.create_time = create_time;
}
public User() {
    super();
}
Copy after login

}

5. Write a class MybatisSamples, which contains the main method for testing.

The specific code is as follows

public static void main(String[] args) {
    String resource = "mybatis-config.xml";
    SqlSession session = null;
    try {
        InputStream is = Resources.getResourceAsStream(resource);
        //获取一个session工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过工厂获取session
        session = sqlSessionFactory.openSession();
        //根据映射文件中select标签的id获取集合
        List<User> list = session.selectList("userMapper.selectUser");
        for(User user: list) {
            System.out.println(user);
            
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        //注意关闭session!!!
        session.close();
    }
    
}
Copy after login

The execution results are as follows:

mysql connects to the database and tests instance sharing

Attachment:
Package structure

mysql connects to the database and tests instance sharing

##Database structure

mysql connects to the database and tests instance sharing

Summary: The two most important steps to get started with mybatis are to write the configuration file, configure the database and mapper, and the second step is to write the mapper and entity class, and write the sql statement in the mapper. Then you can map the data in the table into an object through mybatis.

Related recommendations:

php mysql connection database instance_MySQL

MySQL connection database function memory leak solution

Problems connecting python and mysql to the database

The above is the detailed content of mysql connects to the database and tests instance sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!