<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.33version>
dependency>
# mysql8的驱动
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=root
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties">
properties>
<typeAliases>
<package name="com.cc.demo.model"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="mapper/SeatMapper.xml"/>
<mapper resource="mapper/UserInfoMapper.xml"/>
mappers>
configuration>
create table userinfo
(
userId int auto_increment
primary key,
username varchar(50) not null,
userSex tinyint not null,
userPhone varchar(50) not null,
email varchar(50) not null
);
大家参照这个格式,然后使用自己的表即可
2. 然后根据数据库表创建实体类,如下所示:
public class UserInfo implements Serializable {
private Integer userId;
private String username;
private Byte usersex;
private String userphone;
private String email;
public UserInfo() {
}
public UserInfo(Integer userId, String username, Byte usersex, String userphone, String email) {
this.userId = userId;
this.username = username;
this.usersex = usersex;
this.userphone = userphone;
this.email = email;
}
private static final long serialVersionUID = 1L;
public Integer getuserId() {
return userId;
}
public void setuserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Byte getUsersex() {
return usersex;
}
public void setUsersex(Byte usersex) {
this.usersex = usersex;
}
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
this.userphone = userphone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
@Mapper
public interface UserInfoMapper {
int insert(UserInfo record);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cc.demo.mapper.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.zzj.webservlet.model.UserInfo">
<id column="userId" jdbcType="INTEGER" property="userId" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="userSex" jdbcType="TINYINT" property="usersex" />
<result column="userPhone" jdbcType="VARCHAR" property="userphone" />
<result column="email" jdbcType="VARCHAR" property="email" />
resultMap>
<sql id="Base_Column_List">
userId, username, userSex, userPhone, email
sql>
<insert id="insert" keyColumn="userId" keyProperty="userId" parameterType="com.cc.demo.model.UserInfo" useGeneratedKeys="true">
insert into userinfo (username, userSex, userPhone,
email)
values (#{username,jdbcType=VARCHAR}, #{usersex,jdbcType=TINYINT}, #{userphone,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR})
insert>
mapper>
注意: 以上就是mybatis的主要使用过程,mapper和对应的xml映射文件结合使用,用来操作数据库。mybatis也可以使用注解对数据库进行操作。但xml文件能够实现更为复杂的语句,所以一般我在开发过程中是xml文件和注解配合使用的。
上面只给出了mybatis的配置和xml文件的编写,这里给出具体如何使用mybatis操作数据库。具体代码如下所示:
public class Test01 {
public static SqlSessionFactory getSqlSessionFactory() throws IOException {
//注意此处路径不要写错
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
public static void main(String[] args) throws IOException {
//1、获取SqlSessionFactory实例
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2、打开一个会话
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3、获取接口的实现类对象,会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
UserInfoMapper mapper = openSession.getMapper(UserInfoMapper.class);
int count = 0;
// 4、调用接口的方法
for (int i = 0; i < 30; i++) {
if(i % 2 == 0){
count += mapper.insert(new UserInfo(i,"xiao" + i, (byte) 1,"156382260" +i,"xiao" + i +"@qq.com"));
}else {
count +=mapper.insert(new UserInfo(i,"xiao" + i, (byte) 0, "156382260" +i,"xiao" + i +"@qq.com"));
}
}
System.out.println(count);
//7.提交事务,核心配置文件中的事物方式为JDBC,因此需要手动提交
//这里需要加上commit(),否则能够实现功能,但不能将修改持久化到数据库,也就是说,没有commit不会真正的改变数据库,而是会回滚的执行前
openSession.commit();
} finally {
//4、使用完毕后关闭会话
openSession.close();
}
}
}
上面就是maven项目使用mybatis的过程,大概就是这样一个模板。可以根据自己的需要进行修改,而且上面的创建工厂和会话可以进行封装,不用每次都再去写这一部分的代码。