1.创建maven工程
(1)首先打开idea点击idea中的File->New->Project...
(2)在左侧找到Maven如图所示:
然后点击Next
在配置好工程名后点击Next
然后确认工程路径点击Finish创建。
(3)创建一个数据库和表结构如下:
在添加几条记录:
2.搭建mybatis框架
在src文件夹中创建lib文件夹导入mysql与mybatis的jar包并将包添加到库中。
(1)在Users类中编写如下:
package com.soutwind.entity;
public class Users {
private int id;
private String username;
private String password;
public Users(){
}
public Users(String username,String password){
this.username=username;
this.password=password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
(2)在pom.xml中编写如下代码配置框架信息:
<?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>ma_my</groupId>
<artifactId>ma_my工程</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connection-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
(3)在resources中创建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>
<!-- 配置MyBatis运行环境-->
<environments default="development">
<environment id="development">
<!-- 配置jdbc的事务管理-->
<transactionManager type="JDBC">
</transactionManager>
<!-- pooled配置jdbc数据源连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/190506?useUnicode=true&characterEncoding=GBK"></property>
<property name="username" value="root"></property>
<property name="password" value="x5"></property>
</dataSource>
</environment>
</environments>
<!-- 注册UsersMapper.xml-->
<mappers>
<mapper resource="mapper/UsersMapper.xml"></mapper>
</mappers>
</configuration>
config.xml文件负责配置数据库的连接<environment >配置数据库的连接可以拥有多个数据库的配置,<environments>标签负责决定框架用那个数据库。
(4)在resources中创建Mapper包,在包中创建UsersMapper.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="resources.mapper.UsersMapper">
<insert id="insert" parameterType="com.soutwind.entity.Users">
insert into users(name,password) values(#{username},#{password})
</insert>
<select id="select" resultType="com.soutwind.entity.Users">
select * from users
</select>
<delete id="delete" parameterType="com.soutwind.entity.Users">
delete from users where id=#{id}
</delete>
<update id="update" parameterType="com.soutwind.entity.Users">
update users set name=#{username},password=#{password} where id=#{id}
</update>
</mapper>
UsersMapper.xml文件负责编写sql代码。
3.创建测试类Test并对单表进行增删改查,在其中编写如下代码:
package com.soutwind.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class Test {
public static void main(String[] args){
//加载MyBatis配置文件
InputStream in=Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder der=new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory= der.build(in);
SqlSession sqlSession=sqlSessionFactory.openSession();
String insert="resources.mapper.UsersMapper.insert";
String select="resources.mapper.UsersMapper.select";
String delete="resources.mapper.UsersMapper.delete";
String update="resources.mapper.UsersMapper.update";
//添加记录
/*Users user=new Users("张三","321");
sqlSession.insert(insert,user);*/
//删除记录
/*Users user=new Users();
user.setId(7);
sqlSession.delete(delete,user);*/
//修改记录
/*Users user=new Users("李四","123");
user.setId(5);
sqlSession.update(update,user);*/
//查询记录
/*List<Users> list=sqlSession.selectList(select);
for(int i=0;i<list.size();i++){
Users user=list.get(i);
System.out.println("id:"+user.getId());
}*/
// 提交事务 增删改 需要commit,查询无需commit
sqlSession.commit();
//关闭会话
sqlSession.close();
}
}
最就可以通过测试类对表进行增删改查的操作了啦。
————————————————
版权声明:本文为CSDN博主「weixin_46102505」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46102505/article/details/120251565