Mybatis学习
发现学完东西老忘还得重看一遍视频 所以记录一下
第一步
首先在maven下面加入依赖
<?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>com.com.su</groupId>
<artifactId>Mybatis-Study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-01</module>
<module>mybatis-02youhua</module>
</modules>
<!--导入依赖-->
<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.2.6</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<!-- 打包生成xml-->
<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>
</project>
第二步
在resources下面加入mybatis-config.xml
根据自己需要修改url和用户名密码
<?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"/>
<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/com.su/dao/UserMapper.xml"/>
</mappers>
</configuration>
第三步
创建工具类:Mybatisuils
package com.su.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;
//SqlSessionFactory-->SqlSession 链接数据库
public class Mybatisutils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSessionFactory中获得SqlSession的实例
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
第四步
创建实体类 名字和数据库对上 然后alt+insert 快速设置getset和tostring
package com.su.pojo;
//实体类
public class User {
private int id;
private String name;
private String pwd;
public User(){
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
第五步
创建Usermapper和Mapper.xml
package com.su.dao;
import com.su.pojo.User;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface UserMapper {
List<User> getUserList();//查
List<User> getUserListByName(String value);//模糊查找
User getUserByid(int id);//条件查
User getuserByNameandPwd(Map<String,Object> map);
int addUser(User user);
int addUser2(Map<String,Object> map);
int updateUser(User user);
int deleteUser(User user);
}
<?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.com.su.dao.UserMapper">
<select id="getUserList" resultType="com.com.su.pojo.User" >
select * from user
</select>
<select id="getUserListByName" resultType="com.com.su.pojo.User">
select * from user where name like #{value}
</select>
<select id="getUserByid" parameterType="int" resultType="com.com.su.pojo.User">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="com.com.su.pojo.User">
insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<insert id="addUser2" parameterType="map">
insert into user(id,name,pwd) values (#{userid},#{username},#{userpwd})
</insert>
<select id="getuserByNameandPwd" parameterType="map" resultType="com.com.su.pojo.User">
select * from user where name = #{username} and pwd = #{userpwd}
</select>
<update id="updateUser" parameterType="com.com.su.pojo.User">
update user set name=#{name},pwd=#{pwd} where id = #{id} ;
</update>
<delete id="deleteUser" parameterType="com.com.su.pojo.User">
delete from user where id = #{id}
</delete>
</mapper>
解析一下代码
<mapper namespace="com.com.su.dao.UserMapper"> //绑定对应的Mapper接口
<select id="getUserList" resultType="com.com.su.pojo.User" >//id是找Mapper中对应的方法 List<User> getUserList()
// resultType返回对应类型
超级重要:
每创建一个Mapper.xml就要在mybatis-config.xml加入 注册
<mappers>
<mapper resource="com/com.su/dao/UserMapper.xml"/>
</mappers>
第六步
junit测试
运行一下代码
首先在test里建立个UserDaoTest
package com.su.dao;
import com.su.pojo.User;
import com.su.utils.Mybatisutils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserDaoTest {
@Test
public void test(){
//必写
SqlSession sqlSession= Mybatisutils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// System.out.println(userMapper.getUserByid(1));
// List<User> userList = userMapper.getUserList();
//
// for (User user : userList) {
// System.out.println(user);
// }
// int test = userMapper.addUser(new User(4,"xjz","sb"));
// if (test>0){
// System.out.println("提交陈宫");
// }
// int test = userMapper.updateUser(new User(4,"xjz","zdsb"));
// if (test>0){
// System.out.println("修改陈宫");
// }
//
// int test = userMapper.deleteUser(new User(4,"xjz","zdsb"));
// if (test>0){
// System.out.println("删除陈宫");
// }
// Map map = new HashMap<String,Object>();
// map.put("userid",4);
// map.put("username","sbsbs");
// map.put("userpwd","xjzsb");
// int test = userMapper.addUser2(map);
// if (test>0){
// System.out.println("加入成功");
// }
//
// sqlSession.commit();
//
// Map map = new HashMap<String,Object>();
// map.put("username","sbsbs");
// map.put("userpwd","xjzsb");
// User user = userMapper.getuserByNameandPwd(map);
// System.out.println(user);
List<User> userList = userMapper.getUserListByName("%m%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
如果报错:
检查maven是否有以下语句 不然java文件夹里的xml不会一起导出
<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>