旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

mysql衔接数据库并测试实例分享【MySQL教程】,mysql,数据库,实例

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:61评论:0


导读:本文主要和人人分享mysql衔接数据库并测试实例,希望能协助到人人。1.经由过程maven导入关于mybatis的jar包能够经由过程阿里云的maven库直接下载相...
本文主要和人人分享mysql衔接数据库并测试实例,希望能协助到人人。

1.经由过程maven导入关于mybatis的jar包

能够经由过程阿里云的maven库直接下载相干jar包

<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>

2.编写mybatis的设置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<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>

3.编写映照文件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>

4.写出实体类User,很寻常的类,就不在过多诠释.

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();
}

}

5.在写一个类MybatisSamples,内里包括main要领,举行测试.

细致代码以下

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();
    }
    
}

实行效果以下:

附:
包构造

数据库构造

总结:想入门mybatis最主要的两步,第一是写设置文件,设置数据库和mapper,第二步是写好mapper和实体类,mapper中写sql语句,然后能够就能够经由过程mybatis把表中的数据映照成为一个对象了。

相干引荐:

php mysql衔接数据库实例_MySQL

MySQL衔接数据库函数内存泄漏解决方案

python和mysql衔接数据库的题目

以上就是mysql衔接数据库并测试实例分享的细致内容,更多请关注ki4网别的相干文章!

标签:mysql数据库实例


欢迎 发表评论: