1.配置环境变量:在系统变量下配置
创建Maven
创建父工程:删掉src文件
2.使用maven:
在pom.xml文件下配置MySQL、mybatis、junit
<!--父工程-->
<groupId>org.example</groupId>
<artifactId>mybatis_2</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis_01</module>
</modules>
<!--导入依赖 -->
<dependencies>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
3.在父工程下创建子工程:
在resource创建: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.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="root"/>
</dataSource>
</environment>
</environments>
<!-- 每个Mapper(映射)都需要在Mybatis核心配置文件中注册 -->
<mappers>
<mapper resource="src/main/java/com/dao/UserMapper.xml"/>
</mappers>
</configuration>
4.创建包:java.com.Utils、dao、bean
Bean:对象
package com.Bean;
import org.apache.ibatis.type.Alias;
//起别名
@Alias("User")
public class User {
private int id;
private String uname;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String name) {
this.uname = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + uname + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
public User(int id, String name, String pwd) {
this.id = id;
this.uname = name;
this.pwd = pwd;
}
}
5.Utils:
SqlSessionFactory sql会话工厂 给mapper提供sql会话窗口
package com.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 = null;
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSeesion() {
return sqlSessionFactory.openSession();
}
}
6.Dao:
UserDao接口
package com.dao;
import com.Bean.User;
import java.util.List;
public interface UserMapper {
//模糊查询
List<User> getUserLike(String value);
//查询所有User信息
List<User> getUserList();
//传入id进行查询
User getUserId(int id);
//增加用户
int addUser(User user);
//修改用户信息
int updateUser(User user);
//删除用户信息
int deleteUser(int id);
}
7.UserMapper.xml映射(即UserDao的实现类):
<?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">
<!--namespace 命名空间绑定一个对应的DAO/Mapper接口-->
<mapper namespace="com.dao.UserMapper">
<!-- 当User中的uname与数据库中的name不一致时 -->
<resultMap id="Username" type="User">
<result column="name" property="uname"></result>
</resultMap>
<select id="getUserLike" resultMap="Username">
select * from mybatis.user where name like #{name};
</select>
<select id="getUserList" resultType="User">
select * from user;
</select>
<select id="getUserId" parameterType="int" resultType="User">
select * from user where id=#{id};
</select>
<insert id="addUser" parameterType="User">
insert into user value (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="User">
update user set name=#{name} ,pwd=#{pwd} where id=#{id} ;
</update>
<delete id="deleteUser" parameterType="User">
delete from user where id=#{id};
</delete>
</mapper>