Mybits
需要在maven库中下载4个依赖
并且指定文件位置
src/main/java
**/*.xml
false
mysql
mysql-connector-java
5.1.39
log4j
log4j
1.2.12
commons-logging
commons-logging
1.2
org.mybatis
mybatis
3.4.6
在resources中要有3个配置文件
db.properties
# 右键,属性--》utf-8
# 数据库配置文件 name = value的形式配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=root
log4j.properties
# Global logging configuration
#在开发环境日志级别要设置为DEBUG、生产环境要设置为INFO或者ERROR
log4j.rootLogger=DEBUG, stdout
# 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
SqlMapConfig.xml
查询类.xml文件实现接口中方法
test测试类
package com.neuedu.main;
import com.neuedu.mapper.UserinfoMapper;
import com.neuedu.pojo.Userinfo;
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 Test {
public static void main(String[] args) {
// 调用mapper,完成数据库操作,在控制台打印
// 1.根据id查询用户
// 1.1 构建session工厂
InputStream inputStream=null;
SqlSessionFactory sqlSessionFactory=null;
SqlSession sqlSession=null;
try {
inputStream= Resources.getResourceAsStream("SqlMapConfig.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
// 1.2 创建sqlSession对象--代理对象
sqlSession=sqlSessionFactory.openSession();
// 1.3 通过代理要对应的mapper对象
UserinfoMapper userinfoMapper= sqlSession.getMapper(UserinfoMapper.class);
// 1.4 调用xml文件
Userinfo userinfo=userinfoMapper.getUserfiByid(1);
System.out.println(userinfo.getUid());
System.out.println(userinfo.getName());
System.out.println(userinfo.getAge());
System.out.println(userinfo.getBirthday());
} catch (IOException e) {
e.printStackTrace();
}
}
}
增删改查
test
package com.neuedu.main;
import com.neuedu.mapper.UserinfoMapper;
import com.neuedu.pojo.Userinfo;
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.Date;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
// 调用mapper,完成数据库操作,在控制台打印
// 1.根据id查询某个用户
InputStream inputStream = null;
SqlSessionFactory sqlSessionFactory = null;
SqlSession sqlSession = null;
try {
// 1.1构建session工厂 -- 工厂生成对象
inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 1.2创建sqlSession对象 -- 代理对象
sqlSession = sqlSessionFactory.openSession();
// 1.3通过代理要对应的mapper对象
UserinfoMapper userinfoMapper = sqlSession.getMapper(UserinfoMapper.class);
// 1.4调用方法得到返回值
// Userinfo userinfo = userinfoMapper.getUserinfoById(2);
// System.out.println(userinfo.getId());
// System.out.println(userinfo.getName());
// System.out.println(userinfo.getAge());
// System.out.println(userinfo.getBirthday());
// Map map = userinfoMapper.getUserinfoById2(1);
// System.out.println(map);
// for(Map.Entry entry : map.entrySet()){
// System.out.println(entry.getKey()+","+entry.getValue());
// }
// List list = userinfoMapper.getUserinfoListByName("mike");
// for (Userinfo userinfo : list) {
//// System.out.println(userinfo.getId());
//// System.out.println(userinfo.getName());
//// System.out.println(userinfo.getAge());
//// System.out.println(userinfo.getBirthday());
//// System.out.println("===================================");
//// }
// List
接口
package com.neuedu.mapper;
import com.neuedu.pojo.Userinfo;
import java.util.List;
import java.util.Map;
public interface UserinfoMapper{
// 根据id查询某个用户
Userinfo getUserinfoById(int id);
Map getUserinfoById2(int id);
// 根据姓名查询用户
List getUserinfoListByName(String name);
List
实现xml
insert into userinfo(name,age,birthday)
values (#{name},#{age},#{birthday})
update userinfo
set name = #{name}
where id = #{id}
delete from userinfo
where id = #{value}
update userinfo
name = #{name}
,age = #{age}
where id = #{id}
数据插入时返回主键值
select LAST_INSERT_ID()
insert into user(username, birthday, sex, address)
values(#{username}, #{birthday}, #{sex}, #{address})