Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of mysql connecting to the database and testing

2025-02-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要介绍mysql连接数据库并测试的方式方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1.通过maven导入关于mybatis的jar包

可以通过阿里云的maven库直接下载相关jar包

org.mybatis mybatis 3.4.5 MySQL mysql-connector-Java 5.1.38

2.编写mybatis的配置文件

3.编写映射文件UserMapper.xml

select * from user

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

执行结果如下:

附:

包结构

数据库结构

Summary: want to get started mybatis the most important two steps, the first is to write configuration files, configuration database and mapper, the second step is to write a good mapper and entity classes,mapper write sql statements, and then you can through mybatis to the table data mapping into an object.

The above is "mysql connection database and test methods" all the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report