今天,我捣鼓了下mybatis
-------------------------
mybatis环境搭建
-------------------------
我用的idea
首先需要创建maven项目
然后在pow.xml中加这些来下载依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
这是我的项目结构,没有的文件和文件夹先创建
后面会提供
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTDConfig 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/ssm"/>
<property name="username" value="root"/>
<property name="password" value="zhangjun249"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--自动扫描所在包下的dao接口和xml映射文件-->
<!--这里有坑 maven项目xml要放到resource文件夹中-->
<package name="mapper"/>
</mappers>
</configuration>
*注意看这里有坑
<mapper resource="mapper/>
-----------
这里是指定xml映射文件和dao接口的所在的包
如果创建的是maven项目,xml和dao接口不能放在一起
这里xml我放到了resource 文件夹下的mapper文件夹下了
然后dao接口放到java文件夹下的mapper文件夹下了
-----------------------------------------------------------------
我们先执行sql来创建表
create table t_user(
id int(50),
username varchar(50),
password varchar(50),
intruction varchar(255));
然后创建User类
package bean;
public class User {
Integer id;
String username;
String password;
String intruction;
public User(Integer id, String username, String password, String intruction) {
this.id = id;
this.username = username;
this.password = password;
this.intruction = intruction;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIntruction() {
return intruction;
}
public void setIntruction(String intruction) {
this.intruction = intruction;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", intruction='" + intruction + '\'' +
'}';
}
}
我们可以通过SqlSessionFactoryBuilder和一个指定mybatic配置文件的路径来获得SqlSessionFactory
然后通过这个SqlSessionFactory来获得一个SqlSession
这里我就抽成一个类了
DBTool
package tool;
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 DBTool {
private static SqlSessionFactory factory;
static {
try {
String path = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(path);
factory = new SqlSessionFactoryBuilder().build(is);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSession() throws IOException {
// 每次都重新open会话
return factory.openSession();
}
}
我使用接口来指定要做的业务逻辑
package mapper;
import bean.User;
import java.util.List;
public interface UserMapper {
/**
* 添加用户
*/
void insertUser(User user) throws Throwable;
/**
* 删除用户
*/
void deleteUser(int id)throws Throwable;
/**
* 查询所有用户
*/
List<User> getAllUser()throws Throwable;
User getUser(int id)throws Throwable;
/**
* 修改用户
*/
void modifyUser(User user)throws Throwable;
}
然后写sql映射文件
<?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="mapper.UserMapper">
<insert id="insertUser" parameterType="bean.User">
insert into t_user (id,username,password,intruction) values(#{id},#{username},#{password},#{intruction})
</insert>
<delete id="deleteUser" parameterType="int">
delete from t_user where id=#{id}
</delete>
<select id="getAllUser" resultType="bean.User">
select * from t_user
<where>
<if test="id>5">
And intruction='嘿嘿'
</if>
</where>
</select>
<select id="getUser" parameterType="int" resultType="bean.User">
select * from t_user where id=#{id}
</select>
<update id="modifyUser" parameterType="bean.User">
update t_user set username=#{username},password=#{password},intruction=#{intruction} where id=#{id}
</update>
</mapper>
*这里的<namespace="">如果要与接口对应起来的话,要写指定接口的类全名
这里有四个标签insert,select,delete,update分别对应数据库的插入,查询,删除,修改操作
-->然后每个标签又有id属性,用来标识映射语句,这个id与namaspace中指定的接口中的方法对应
-->resultType就是指定传递的参数的类型
-->id=#{id}这里是接收方法传递的叫做id的变量,然后将对应的值接收过来拼接成字符串(假如方法传递了5,那么这里就变成id=5).如果我们传递的是一个User对象,我们还可以直接使用传递的对象里面指定名字的变量名的值(见最后一个语句update)
-->where标签用于指定条件,通过条件生成动态sql,在where标签中加if标签,然后test就对应条件,当满足条件的时候,就将被if包围的字符串拼接到sql后面(mybatis做了处理,如果当前满足的条件是第一个,就会去掉and,避免尴尬)
这是测试代码
import bean.User;
import server.UserServer;
public class Content {
public static void main(String[] args) {
UserServer.insertUser(new User(8, "李青", "a10000236d69ff", "嘿嘿"));
UserServer.modifyUser(new User(8, "张君", "传智学院", "哈"));
System.out.println(UserServer.getAllUser().size());
User user = (User) UserServer.getUserByID(8);
System.out.println(user);
UserServer.deleteUser(8);
}
}
然后跑通了