我这里使用的是IDEA开发工具,介绍一下怎样使用MyBatis连接数据库。
一,我们创建一个新的工作空间,
选择Maven项目
创建完成之后,配置Maven的路径,点击上方工具栏的扳手图标,然后搜索栏中输入maven,修改maven的路径,注意下面的两个文件的路径也要修改。
二,创建好文件后,首先来到pom.xml文件中,这里可以配置需要用到的jar包,然后点击右下角弹出的Impor Changes导入修改的信息.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jdbc</groupId> <artifactId>MavenDemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
三,在resources文件夹中创建一个batabase.properties文件,用于配置连接数据库的相关信息.
注意数据库的路径,用户名和密码要用自己的,不要写错.
然后在resources文件夹中创建一个mybatis-config.xml文件,用于配置环境.
<?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> <!--导入datebase.properties,配置连接数据库的相关信息,优化代码结构,方便维护--> <properties resource="datebase.properties"/> <!--environments 配置环境,其中可以有多个环境--> <environments default="dev"> <!--environment 环境变量--> <environment id="dev"> <!--transactionManager 事务管理器 ,采用JDBC事务--> <transactionManager type="JDBC"></transactionManager> <!--dataSource 数据源,sPOOLED是mybatis自带的数据源--> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!--mappers 映射器,将mapper文件加入到配置文件中--> <mappers> <mapper resource="com/bdqn/dao/TUserMapper.xml"/> </mappers> </configuration>
四,在java文件夹中创建包com.bdqn.entity,在其中创建实体类Tuser,类中的属性与数据库中对应.并将其封装
package com.bdqn.entity; /** * @param * @Author: * @Description: * @Date: */ public class Tuser { private Integer Id ; private String userName,password,nickName; @Override public String toString() { return "Tuser{" + "Id=" + Id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", nickName='" + nickName + '\'' + '}'; } public Integer getId() { return Id; } public void setId(Integer id) { Id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } }五,在java文件夹中创建包com.bdqn.dao,并在其中创建Mapper映射文件TUserMapper.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="com.bdqn.dao.TUserMapper"> <resultMap id="userMap" type="com.bdqn.entity.Tuser"> <!--这里是与数据库的映射文件--> <id column="id" property="Id"/> <result column="user_name" property="userName"/> <result column="password" property="password"/> <result column="nick_name" property="nickName"/> </resultMap> <!--查询t_user表中的所有信息--> <select id="queryCount" resultType="int"> SELECT COUNT(*) FROM t_user </select> </mapper>六,在test.java文件中创建测试类UserTest.java
import com.bdqn.dao.TUserMapper; import com.bdqn.entity.Tuser; import com.bdqn.util.MyBatisUtil; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class UserTest { @Test public void testQueryAll(){ List<Tuser> list = null; SqlSession sqlSession = null; try { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); list = sqlSession.selectList("com.bdqn.dao.TUserMapper.queryAll"); } catch (IOException e) { e.printStackTrace(); } finally { if(sqlSession!=null){ sqlSession.close(); } } if (list!=null){ for (Tuser tuser : list) { System.out.println(tuser); } } } }
运行结果:
运行成功了,以上就是是用MyBatis连接数据库的方法,本人是菜鸟一枚,学习的还不到位,若有不对的地方欢迎指正.