创建一个默认的Maven工程,不需要勾选任何一项模板
设置项目名称,以及所属单位,点击finish创建完成
所需要的依赖有mybatis核心依赖包,MySQL驱动包,以及为了方便测试需要的junit包,在下面弹出的提示框中点击Enable Auto-Import,表示自动导入需要的依赖
<configuration>
<properties resource="mybatis.properties">properties>
<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>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
mappers>
configuration>
username=root
password=1234
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&useSSL=false
driver=com.mysql.jdbc.Driver
-- ----------------------------
-- Table structure for commodity
-- ----------------------------
DROP TABLE IF EXISTS `commodity`;
CREATE TABLE `commodity` (
`id` int(11) NOT NULL,
`name` varchar(55) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`madein` varchar(55) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`inprice` int(11) NOT NULL,
`outprice` int(11) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of commodity
-- ----------------------------
INSERT INTO `commodity` VALUES (1, '鱼豆腐', '中国', 2, 3);
INSERT INTO `commodity` VALUES (2, '辣条', '中国', 5, 10);
INSERT INTO `commodity` VALUES (3, '辣条', '中国', 5, 10);
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(55) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(55) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '西瓜', '123');
INSERT INTO `user` VALUES (2, '橘子', '123');
package com.youzi;
public class User {
private Integer id;
private String name;
private String password;
public User() {
}
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
<?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指定Dao接口的完整类名-->
<!--mybatis会依据这个接口动态创建一个实现类去实现这个接口,而这个实现类是一个Mapper对象-->
<!--namespace值包名的命名空间-->
<mapper namespace="com.youzi.model">
<resultMap type="com.youzi.model.User" id="userMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="password" property="password"/>
</resultMap>
<!--查询语句-->
<select id="findUserById" parameterType="int" resultMap="userMap">
select * from user where id = #{id}
</select>
<!--新增语句-->
<insert id="istUser" parameterType="com.youzi.model.User">
insert into user values(#{id},#{name},#{password})
</insert>
<!--删除语句-->
<delete id="delUserById" parameterType="int">
delete from user where id=#{id}
</delete>
<!--更新语句-->
<update id="uptUserById" parameterType="com.youzi.model.User">
update user set name=#{name},password=#{password} where id=#{id}
</update>
</mapper>
package Test;
import com.youzi.model.User;
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 org.junit.Test;
import java.io.Reader;
public class UserTest {
@Test
public void findUserById(){
//mybatis-config.xml文件路径
String resources = "mybatis-config.xml";
SqlSession sqlSession = null;
try{
Reader reader = Resources.getResourceAsReader(resources);
//获取sqlSession
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();
//执行sql语句
User user = sqlSession.selectOne("com.youzi.model.User.findUserById",1);
System.out.println(user);
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭sqlSession
if(sqlSession!=null){
sqlSession.close();
}
}
}
}