pom.xml:
<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.0modelVersion>
<groupId>textgroupId>
<artifactId>MybatisartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.16.20version>
<scope>providedscope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.25version>
<scope>testscope>
dependency>
dependencies>
project>
Mybatis.xml:
<configuration>
<properties resource="db.properties"/>
<typeAliases>
、
<package name="text.Mybatis.bean"/>
typeAliases>
<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="text/Mybatis/dao/UserMapper.xml">mapper>
mappers>
configuration>
log4j.properties:
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
db.properties:
diver=com.mysql.jdbc.Driver
url=jjdbc:mysql:///mybatis
value=root
password=123
UserMapper.xml:
<mapper namespace="text.Mybatis.dao.IUserDao">
<resultMap id="userResultMap" type="text.Mybatis.bean.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="birthday" property="birthday"/>
<result column="salary" property="salalry"/>
resultMap>
<insert id="save" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into user(name,age,birthday,salary)
VALUES (#{name},#{age},#{birthday},#{salary})
insert>
<update id="update">
update user set
name=#{name},
age=#{age},
birthday=#{birthday},
salary=#{salary}
WHERE
id=#{id}
update>
<delete id="delete">
delete from user where id=#{id}
delete>
<select id="get" resultMap="userResultMap">
select * from user where id=#{id}
select>
<select id="list" resultType="User">
select * from user
select>
mapper>
App.java:
import text.Mybatis.bean.User;
import text.Mybatis.dao.IUserDao;
import text.Mybatis.dao.impl.UserDaoImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.apache.ibatis.io.Resources;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
/**
* Created by Administrator on 2019/8/28.
*/
public class App {
@Test
public void testSqlSessionFactory() throws Exception {
//获取SQLSessionFactory对象
String resource="Mybatis.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sqlSessionFactory.openSession();
System.out.println(sqlSessionFactory);
}
@Test
public void testSave() throws Exception {
IUserDao dao=new UserDaoImpl();
User user=new User(null,"jack",20,new Date(1000),new BigDecimal(3000.00));
dao.save(user);
}
@Test
public void testUpdate() throws Exception {
IUserDao dao=new UserDaoImpl();
User user=new User(2,"tom",30,new Date(),new BigDecimal(5000.00));
dao.update(user);
}
@Test
public void testDelete() throws Exception {
IUserDao dao=new UserDaoImpl();
dao.delete(2);
}
@Test
public void testGet() throws Exception {
IUserDao dao=new UserDaoImpl();
System.out.println(dao.get(1));
}
@Test
public void testList() throws Exception {
IUserDao dao=new UserDaoImpl();
System.out.println(dao.list());
}
}
MybatisUtil.java:
package text.Mybatis.util;
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;
/**
* Created by Administrator on 2019/8/28.
*/
public class MybatisUtil {
public static SqlSessionFactory factory=null;
static {
String resource="Mybatis.xml";
InputStream in = null;
try {
in = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
factory = new SqlSessionFactoryBuilder().build(in);
}
public static SqlSession getsession (){
return factory.openSession();
}
}
IUserDao.java(接口):
package text.Mybatis.dao;
import text.Mybatis.bean.User;
import java.util.List;
/**
* Created by Administrator on 2019/8/28.
*/
public interface IUserDao {
void save(User user);
void update(User user);
void delete(Integer id);
User get(Integer id);
List<User> list();
}
UserDaoImpl.java:
package text.Mybatis.dao.impl;
import text.Mybatis.bean.User;
import text.Mybatis.dao.IUserDao;
import text.Mybatis.util.MybatisUtil;
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;
import java.util.List;
/**
* Created by Administrator on 2019/8/28.
*/
public class UserDaoImpl implements IUserDao{
public void save(User user) {
//执行保存操作
// 获取SqlSession对象
SqlSession session = MybatisUtil.getsession();
// 执行保存的sql语句
// namespace的值加上id值
session.insert("text.Mybatis.dao.IUserDao.save",user);
// 提交事务
session.commit();
// 关闭session
session.close();
}
// 更新操作
public void update(User user) {
SqlSession session = MybatisUtil.getsession();
session.update("text.Mybatis.dao.IUserDao.update",user);
session.commit();
session.close();
}
public void delete(Integer id) {
SqlSession session = MybatisUtil.getsession();
session.delete("text.Mybatis.dao.IUserDao.delete",id);
session.commit();
session.close();
}
public User get(Integer id) {
SqlSession session = MybatisUtil.getsession();
// 查询一个
User user = (User) session.selectOne("text.Mybatis.dao.IUserDao.get", id);
session.commit();
session.close();
return user;
}
public List<User> list() {
SqlSession session = MybatisUtil.getsession();
// 查询列表
List<User> list = session.selectList("text.Mybatis.dao.IUserDao.list");
session.commit();
session.close();
return list;
}
}
User.java:
package text.Mybatis.bean;
import lombok.*;
import org.apache.ibatis.type.Alias;
import java.math.BigDecimal;
import java.util.Date;
/**
* Created by Administrator on 2019/8/28.
*/
@Setter@Getter@ToString
@AllArgsConstructor
@NoArgsConstructor
//可以使用注解显示的配置某个类的别名
@Alias("myUser")
public class User {
private Integer id;
private String name;
private Integer age;
private Date birthday;
private BigDecimal salary;
}