从零开始Mybatis连接数据库的方法
赤叶秋枫 人气:0从零开始Mybatis连接数据库
创建Maven文件
File-->new-->project-->maven,点击next
配置
在出现的pom.xml文件中<project>标签之间加入以下内容
<!--打包方式--> <packaging>jar</packaging> <dependencies> <!--加载Mybatis核心--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!--Junit测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency> </dependencies>
在src-->main-->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> <!--配置连接数据库环境--> <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/test"/> <property name="username" value="自己的用户名"/> <property name="password" value="自己的密码"/> </dataSource> </environment> </environments> </configuration>
注意将username和password的value值改成自己的,且在自己的数据库中创建test库
创建实体类
在main的java下创建我们需要与表中对应的实体类,这里我们写user类
public class User { //实体类 //属性与表中的各项对应,加入相应的get,set方法 int userid; String username; String userpass; String usertel; int usercarad; public User() { } public User(int userid, String username, String userpass, String usertel, int usercarad) { this.userid = userid; this.username = username; this.userpass = userpass; this.usertel = usertel; this.usercarad = usercarad; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public String getUsertel() { return usertel; } public void setUsertel(String usertel) { this.usertel = usertel; } public int getUsercarad() { return usercarad; } public void setUsercarad(int usercarad) { this.usercarad = usercarad; } }
创建com.mapper包,并在包下创建UserMapper接口
这里我们简单做一个添加内容示例
package com.mapper; public interface UserMapper { int insertUser();//返回值为改变的行数 }
创建UserMapper.xml映射文件
在resources文件夹下创建mapper文件夹(包)并在其中创建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"> <!--第一个一致,namespace对应UserMapper接口全类名--> <mapper namespace="com.mapper.UserMapper"> <!--第二个一致,id对应方法名 int insertUser(); --> <insert id="insertUser"> insert into user values (null,"tom","1234","111",22) </insert> </mapper>
在mybatis-config.xml下<configuration>标签中最下面加入
<!--引入映射文件--> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers>
创建测试类
创建Test类进行测试
import com.mapper.UserMapper; 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 java.io.IOException; import java.io.Reader; public class Test { public static void main(String[] args) throws IOException { //获取配置文件 Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); // 获取SqlSessionFactoryBuilder获取SqlSessionFactory工厂 SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); //获取SqlSessionFactory工厂 SqlSessionFactory sqlSessionFactory= sqlSessionFactoryBuilder.build(reader); //利用工厂获取sqlSession SqlSession sqlSession=sqlSessionFactory.openSession();//openSession()加入参数true,即可实现自动提交事务 //利用sqlSession的方法获取接口的对象 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //调用接口方法,从而调用映射文件的sql语句 int num= userMapper.insertUser(); //提交事务 sqlSession.commit(); System.out.println("改变行数"+num); } }
运行结果:
加载全部内容