目录
一、准备工作
1、搭建数据库
2、导入pom.xml文件?
3、解决Maven静态资源过滤问题
4、编写MyBatis核心配置文件
5、编写MyBatis工具类
6、创建实体类
7、编写Mapper.xml配置文件
二、查询操作
1、在UserMapper中添加对应方法
2、在UserMapper.xml中添加Select语句
3、测试类中测试
4、说明
?三、添加操作
1、在UserMapper接口中添加对应的方法
2、在UserMapper.xml中添加insert语句
3、测试
四、修改操作
1、在UserMapper接口中添加对应的方法
?2、在UserMapper.xml中添加update语句
3、测试?
五、删除操作
1、在UserMapper接口中添加对应的方法
?2、在UserMapper.xml中添加delete语句
3、测试
六、小结
一、准备工作
1、搭建数据库
CREATE DATABASE `mybatis`;
USE `mybatis`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(20) NOT NULL,
`name` varchar(30) DEFAULT NULL,
`pwd` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `user`(`id`,`name`,`pwd`) values (1,'王二','123456'),(2,'张三','abcdef'),(3,'李四','987654');
2、导入pom.xml文件?
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
3、解决Maven静态资源过滤问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
4、编写MyBatis核心配置文件
<?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"/>
<!--useUnicode使用Unicode中文编码-->
<!--不加这useUnicode=true&characterEncoding=UTF-8存中文进去是问号-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="12345"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/UseMapper.xml"></mapper>
</mappers>
</configuration>
5、编写MyBatis工具类
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
InputStream inputStream = null;
try {
//获得SqlSessionFactory对象
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 org.apache.ibatis.jdbc.SQL 命令所需的所有方法。
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
6、创建实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private int id;
private String name;
private String password;
}
7、编写Mapper.xml配置文件
配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致!
<?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.dao.UserMapper">
</mapper>
二、查询操作
1、在UserMapper中添加对应方法
public interface UserMapper {
//使用@Param 根据密码和名字查询用户
User selectUserByNP(@Param("username") String username,@Param("pwd") String pwd);
//使用Map 根据密码和名字查询用户
User selectUserByNP2(Map<String,Object> map);
}
2、在UserMapper.xml中添加Select语句
<select id="selectUserByNP" resultType="com.pojo.User">
select * from user where name = #{username} and pwd = #{pwd}
</select>
<select id="selectUserByNP2" parameterType="map" resultType="com.pojo.User">
select * from user where name = #{username} and pwd = #{pwd}
</select>
3、测试类中测试
@Test
public void selectUserByNP2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap();
map.put("id",4);
map.put("name","小原");
User user = mapper.selectUserByNP2(map);
System.out.println(user);
sqlSession.close();
}
4、说明
在接口方法的参数前加 @Param属性,Sql语句编写的时候,直接取@Param中设置的值即可,不需要单独设置参数类型。
在接口方法中,参数直接传递Map,Map的 key 为 sql中取的值即可,没有顺序要求!
?三、添加操作
1、在UserMapper接口中添加对应的方法
// 添加一个用户
int addUser(User user);
2、在UserMapper.xml中添加insert语句
<insert id="addUser" parameterType="com.pojo.User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
3、测试
注意点:增、删、改操作需要提交事务!
@Test
public void testAddUser() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = new User(5,"王五","zxcvbn");
int i = mapper.addUser(user);
System.out.println(i);
session.commit(); //提交事务,重点!不写的话不会提交到数据库
session.close();
}
四、修改操作
1、在UserMapper接口中添加对应的方法
//修改一个用户
int updateUser(User user);
?2、在UserMapper.xml中添加update语句
<update id="updateUser" parameterType="com.pojo.User">
update user set name=#{name},pwd=#{pwd} where id = #{id}
</update>
3、测试?
@Test
public void testUpdateUser() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectUserById(1);
user.setPwd("asdfgh");
int i = mapper.updateUser(user);
System.out.println(i);
session.commit(); //提交事务,重点!不写的话不会提交到数据库
session.close();
}
五、删除操作
1、在UserMapper接口中添加对应的方法
//根据id删除用户
int deleteUser(int id);
?2、在UserMapper.xml中添加delete语句
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
3、测试
@Test
public void testDeleteUser() {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.deleteUser(5);
System.out.println(i);
session.commit(); //提交事务,重点!不写的话不会提交到数据库
session.close();
}
六、小结
|