1、构建环境
<!--在build中配置文件resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2、编写配置文件
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydatabase?
username=root
password=123456
Mybatis-config.xml
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<!--jdbc文件-->
<environments default="development">
<environment id="development">
<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>
</configuration>
3、编写工具类,以便复用
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "Mybatis-config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
//设置为true,为默认提交事务
return sqlSessionFactory.openSession(true);
}
}
4、编写实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
5、编写实体类的dao接口
//注解开发
public interface UserMapper {
//增 类型为user
@Insert("insert into mydatabase.user (id,name,pwd) values(#{id},#{name},#{pwd})")
int add(User user);
//删 类型为int
@Delete("delete from mydatabase.user where id=#{id}")
int delete(int id);
//改 类型为user
@Update("update mydatabase.user set name=#{name},pwd=#{pwd} where id=#{id}")
int update(User user);
//查 类型为int
@Select("select * from mydatabase.user where id=#{id}")
User query(int id);
@Select("select * from mydatabase.user")
List<User> getAll();
}
6、在配置文件中注册Mapper
必须用class注册
<mappers>
<mapper class="com.dao.UserMapper"/>
</mappers>
7、测试
public class test {
@Test//增
public void test1(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.add(new User(6, "小红", "66"));
for (User user : mapper.getAll()) {
System.out.println(user);
}
}
@Test//删
public void test2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.delete(6);
for (User user : mapper.getAll()) {
System.out.println(user);
}
}
@Test//改
public void test3(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.update(new User(1, "张三", "101"));
for (User user : mapper.getAll()) {
System.out.println(user);
}
}
@Test//查
public void test4(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.query(1);
System.out.println(user);
}
}
8、特点
方便,不用去配置接口的实现类,但是接口和接口实现类写在了一起,不方便维护。
最好还是另写一个接口实现类,以便维护的时候方便寻找