IDEA+Maven配置Mybatis——第一个Mybatis程序
Mybatis是一个数据持久层框架,通过配置文件或者注解的方式代替了繁琐的JDBC代码将sql和代码分离,提高了可维护性;支持编写动态sql…
1、在maven仓库中找到Mybatis的依赖并添加到项目依赖中
maven仓库地址:https://mvnrepository.com/
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
版本可根据自身需求进行变动
还需要导入数据库相关的jar包,我用的是mysql8.0.25
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
还有一个很重要的!!!!
maven依赖中添加build标签,不要过滤掉项目中所有文件夹下的xml、properties等配置文件
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2、配置mybatis核心配置文件Mybatis-config.xml
在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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/chen/dao/UserMapper.xml"/>
</mappers>
</configuration>
关于xml标签,常用的有properties ,settings ,typeAliases ,environments ,mappers ,等等,上面一段配置是最简单的
3、连接数据库,并创建一张简单表
连接数据库不必多说,创建的简单表如下
4、dao、pojo、utils包以及对应的类
项目结构如图:
DAO层:
package com.chen.dao;
import com.chen.pojo.User;
import java.util.List;
import java.util.Map;
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
void addUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
- UserMapper.xml(Mybatis配置文件之一)
<?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.chen.dao.UserMapper">
<resultMap id="UserMap" type="User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserList" resultMap="UserMap">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="UserMap">
select * from mybatis.user where id = ${id}
</select>
<insert id="addUser" parameterType="UserMap">
insert into mybatis.user (id,name,pwd) values(${id},'${name}','${pwd}')
</insert>
<update id="updateUser" parameterType="UserMap">
update mybatis.user set name='${name}',pwd='${pwd}' where id=${id};
</update>
<delete id="deleteUser" parameterType="UserMap">
delete from mybatis.user where id=${id};
</delete>
</mapper>
pojo实体类包
package com.chen.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
注:@Data ,@AllArgsConstructor , @NoArgsConstructo 是lombok的注解,这里不写也没什么关系,但是遵循规范偷点懒就用lombok啦~。lombok是idea自带的一个插件,在设置-插件中就有。通过简单的注解就可以自动生成get、set方法、无参、有参构造器等,灰常方便!
utils工具类包
package com.chen.utils;
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.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
5、编写测试类
package com.chen.dao;
import com.chen.pojo.User;
import com.chen.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
public class UserMapperTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(4,"lucy","123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"will","123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
注:@Test 是junit的注解,作用域为方法,可以单元执行方法,常用于测试,在maven中导入依赖即可。也可以写一个main方法来测试
5、最后
Mybatis第一个基本的程序就基本完成了,最重要的还是核心文件mybatis-config.xml ,如果持续学习的话还会和他打交道。以上就是xml配置的基本mybatis程序!
|