org.mybatis
mybatis
3.5.1
mysql
mysql-connector-java
8.0.11
package com.bobo.pojo;
/**
* 多列操作数据
* 按住Alt键 鼠标操作
*/
public class User {
private Integer id;
private String username;
private String address;
private String gender ;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public User() {
}
public User(Integer id, String username, String address, String gender) {
this.id = id;
this.username = username;
this.address = address;
this.gender = gender;
}
public User(String username, String address, String gender) {
this.username = username;
this.address = address;
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
insert into t_user (username,address,gender)values(#{username},#{address},#{gender})
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
}
src/main/java
**/*.xml
true
jdbc:mysql://localhost:3306/shop?
serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
// 5.MyBatis中默认是不会自动提交DML操作的数据的
sqlSession.commit();
// 6.关闭会话
sqlSession.commit();
}
insert into t_user (username,address,gender)values(#{username},#{address},#{gender})
update t_user set username=#{username} ,address=#{address},gender=#{gender} where id =#{id}
delete from t_user where id=#{id}
package com.bobo.test;
import com.bobo.pojo.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.jupiter.api.Test;
import java.io.InputStream;
import java.util.List;
public class TestDemo01 {
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
// 5.MyBatis中默认是不会自动提交DML操作的数据的
sqlSession.commit();
// 6.关闭会话
sqlSession.commit();
}
@Test
public void test02() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
User user = new User(19,"翠花","北京海淀","女");
int i = sqlSession.update("com.bobo.pojo.User.updateUser", user);
System.out.println("影响的行数:" + i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
int i = sqlSession.delete("com.bobo.pojo.User.deleteUser", 20);
System.out.println("影响的行数:" + i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void queryAll() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List lists = sqlSession.selectList("com.bobo.pojo.User.queryAll");
for (User user : lists) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void queryById() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List lists = sqlSession.selectList("com.bobo.pojo.User.queryById",19);
for (User user : lists) {
System.out.println(user);
}
sqlSession.close();
}
}
package com.bobo.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.Date;
/**
* 工具类
* 对外提供SqlSessionFactory的单例对象
*/
public class DbUtils {
private static SqlSessionFactory factory;
public static SqlSessionFactory getInstance(){
if(factory == null){
InputStream in = null;
try{
in = Resources.getResourceAsStream("mybatis-cfg.xml");
}catch (Exception e){
e.printStackTrace();
}
synchronized (DbUtils.class){
if(factory == null){
factory = new SqlSessionFactoryBuilder().build(in);
}
}
}
return factory;
}
}
package com.bobo.dao;
import com.bobo.pojo.User;
import java.util.List;
public interface IUserDao {
int addUser(User user);
int updateUser(User user);
int deleteUser(Integer id);
List queryById(Integer id);
}
package com.bobo.dao.impl;
import com.bobo.pojo.User;
import com.bobo.dao.IUserDao;
import com.bobo.utils.DbUtils;
import java.util.List;
public class UserDaoImpl implements IUserDao {
@Override
public int addUser(User user) {
return DbUtils.getInstance().openSession().insert("com.bobo.dao.IUserDao.addUser",user);
}
@Override
public int updateUser(User user) {
return DbUtils.getInstance().openSession().insert("com.bobo.dao.IUserDao.updateUser",user);
}
@Override
public int deleteUser(Integer id) {
return DbUtils.getInstance().openSession().insert("com.bobo.dao.IUserDao.updateUser",id);
}
@Override
public List queryById(Integer id) {
return DbUtils.getInstance().openSession().selectList("com.bobo.dao.IUserDao.queryById", id);
}
}
@Test
public void test03(){
// 获取一个Dao接口的代理对象
IUserDao dao = (IUserDao) Proxy.newProxyInstance(IUserDao.class.getClassLoader()
, new Class[]{IUserDao.class}
, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(IUserDao.class.getName()+"." + method.getName());
return DbUtils
.getInstance()
.openSession()
.selectList(IUserDao.class.getName()+"." + method.getName(),args[0]);
}
});
// dao.addUser(null);
//dao.updateUser(null);
List list = dao.queryById(18);
for (User user : list) {
System.out.println(user);
}
}
package com.bobo.dao;
import com.bobo.pojo.User;
/**
* Dao的接口
*/
public interface UserMapper {
int addUser(User user);
}
insert into t_user (username,address,gender)values(#{username},#{address},#{gender})
package com.bobo;
import com.bobo.dao.UserMapper;
import com.bobo.pojo.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 java.io.InputStream;
public class AppStart {
public static void main(String[] args) throws Exception{
// 1.加载全局配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.根据SqlSessionFactory获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User("张三","广州","男");
// 通过SqlSession获取Dao接口的代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(user);
// 5.提交资源并关闭连接
sqlSession.commit();
sqlSession.close();
}
}
configuration 配置
properties 属性
settings 设置
typeAliases 类型别名
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
environments 环境
environment 环境变量
transactionManager 事务管理器
dataSource 数据源
databaseIdProvider 数据库厂商标识
mappers 映射器
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/shop?
serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
package com.bobo.typehandler;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/**
* 实现集合和Varchar之间的转换
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListVarcharTypeHandler extends BaseTypeHandler> {
/**
* 插入数据的时候要实现的数据转换是
* List 转换为 字符串
* String sql = "";
* PreparedStatement ps = conn.createPreparedStatement(sql);
* ps.setInt(1,14);
* ps.setString(2,"aaa");
* ps.setString(3,list.toString());
* ps.setString(4,"湖南长沙");
* 。。。
* ps.executeUpdate();
* 。。。
* @param ps
* @param i 执行的SQL语句要插入参数的位置
* @param list 要插入的位置的数据的List类型
* @param jdbcType
* @throws SQLException
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List list, JdbcType jdbcType) throws SQLException {
System.out.println("setNonNullParameter ..... "); // a;b;c;d;
// 处理数据 将List转换为特定格式的字符串
StringBuffer sb = new StringBuffer();
String msg = null;
if(list != null){
for (String p : list) {
sb.append(p).append(";");
}
msg = sb.toString();
msg = msg.substring(0,msg.length()-1);
}
// 给对应位置的占位符赋值
ps.setString(i,msg);
}
/**
* 获取非空的结果集
* @param rs
* @param columnName
* @return
* @throws SQLException
*/
@Override
public List getNullableResult(ResultSet rs, String columnName) throws SQLException {
String msg = rs.getString(columnName);
if(msg!= null){
return Arrays.asList(rs.getString(columnName).split(";"));
}
return null;
}
@Override
public List getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return Arrays.asList(rs.getString(columnIndex).split(";"));
}
@Override
public List getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return Arrays.asList(cs.getString(columnIndex).split(";"));
}
}
insert into t_user (username,address,gender,favorites)values(#
{username},#{address},#{gender},#{favorites})
org.slf4j
slf4j-log4j12
1.7.25
log4j.rootCategory=All, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\\logs\\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
package com.bobo;
import com.bobo.dao.UserMapper;
import com.bobo.pojo.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.apache.log4j.Logger;
import java.io.InputStream;
import java.util.List;
public class AppStart1 {
static Logger logger = Logger.getLogger(AppStart1.class);
public static void main(String[] args) throws Exception{
logger.error("log:error");
logger.warn("log:warn");
logger.info("log:info");
logger.debug("log:debug");
// 1.加载全局配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.根据SqlSessionFactory获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 通过SqlSession获取Dao接口的代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List list = mapper.query(26);
for (User user : list) {
System.out.println(user + ":" + user.getFavorites());
}
// 5.提交资源并关闭连接
sqlSession.commit();
sqlSession.close();
}
}
public interface UserMapper {
int addUser(User user);
List query(@Param("id") Integer id);
}
update t_user set username = #{username} where id = #{id}
package com.bobo.dao;
import com.bobo.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Dao的接口
*/
public interface UserMapper {
int addUser(User user);
List query(@Param("id") Integer id);
int updateUser(String userName,Integer id);
}
package com.bobo;
import com.bobo.dao.UserMapper;
import com.bobo.pojo.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 java.io.InputStream;
import java.util.Arrays;
public class AppStart3 {
public static void main(String[] args) throws Exception{
// 1.加载全局配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.根据SqlSessionFactory获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 通过SqlSession获取Dao接口的代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser("赵六",27);
// 5.提交资源并关闭连接
sqlSession.commit();
sqlSession.close();
}
}
package com.bobo.pojo;
/**
* 包装类
*/
public class UserWrapper {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
insert into t_user (username,address,gender,favorites)values(#{name},#{address},#{gender},#{favorites})
select LAST_INSERT_ID()
insert into t_user(username,address,gender)values(#{user.name},#{user.address},#{user.gender})
public interface UserMapper {
List query(User user);
}
update
t_user
username = #{username},
address = #{address},
gender = #{gender},
where id = #{id}
update
t_user
username = #{username},
address = #{address},
gender = #{gender},
where id = #{id}
update
t_user
,username = #{username}
,address = #{address}
,gender = #{gender}
where id = #{id}
List queryByIds(@Param("ids") List ids);
insert into t_user (username,address) values
(#{user.username},#{user.address})
Integer insertUser(@Param("users") List users);
CREATE TABLE `t_dept` (
`deptid` int NOT NULL AUTO_INCREMENT,
`dname` varchar(20) DEFAULT NULL,
`ddesc` varchar(50) DEFAULT NULL,
PRIMARY KEY (`deptid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
CREATE TABLE `t_emp` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int DEFAULT NULL,
`deptid` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
package com.bobo.pojo;
public class Emp {
private Integer id;
private String name;
private Integer age;
// 1对1关联关系的体现 一个员工对象最多有一个部门对象
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.bobo.pojo;
import java.util.List;
public class Dept {
private Integer deptid;
private String dname;
private String ddesc;
// 1对多 关系 1个部门包含有多个员工对象
private List emps;
public Integer getDeptid() {
return deptid;
}
public void setDeptid(Integer deptid) {
this.deptid = deptid;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDdesc() {
return ddesc;
}
public void setDdesc(String ddesc) {
this.ddesc = ddesc;
}
public List getEmps() {
return emps;
}
public void setEmps(List emps) {
this.emps = emps;
}
@Override
public String toString() {
return "Dept{" +
"deptid=" + deptid +
", dname='" + dname + '\'' +
", ddesc='" + ddesc + '\'' +
'}';
}
}
package com.bobo.dao;
import com.bobo.pojo.Emp;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface EmpMapper {
List queryEmp();
}
# 查询部门信息 然后关联查询出部门中包含的相关的员工信息
SELECT
t1.`deptid`
,t1.`dname`
,t1.`ddesc`
,t2.`id`
,t2.`name`
,t2.age
FROM t_dept t1
LEFT JOIN t_emp t2
ON t1.`deptid` = t2.`deptid`
@Test
public void fun1() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 事务开启
SqlSession sqlSession = factory.openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
// 第一次查询员工信息 一级缓存中是没有数据的,所以会直接查询数据库中的数据
// 然后会将数据保存到一级缓存中
List emps = mapper.queryEmp();
for (Emp emp : emps) {
System.out.println(emp+" : " + emp.getDept());
}
System.out.println("----------------------");
// 第二次查询 因为一级缓存中已经存在了要查询的数据,所以就直接返回了,没有数据库查询操作
emps = mapper.queryEmp();
for (Emp emp : emps) {
System.out.println(emp+" : " + emp.getDept());
}
// 事务关闭 会清空一级缓存的数据
sqlSession.close();
// 重新开启事务
sqlSession = factory.openSession();
mapper = sqlSession.getMapper(EmpMapper.class);
// 因为是新开的事务,一级缓存中的数据是空的,所以会直接查询数据库中的数据
emps = mapper.queryEmp();
for (Emp emp : emps) {
System.out.println(emp+" : " + emp.getDept());
}
sqlSession.close();
}
net.sf.ehcache
ehcache
1.5.0
org.mybatis.caches
mybatis-ehcache
1.1.0
@Test
public void fun1() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 事务开启
SqlSession sqlSession = factory.openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
// 第一次查询员工信息 一级二级缓存中是没有数据的,所以会直接查询数据库中的数据
// 然后会将数据保存到一级缓存和二级缓存中
List emps = mapper.queryEmp();
for (Emp emp : emps) {
System.out.println(emp+" : " + emp.getDept());
}
System.out.println("----------------------");
// 第二次查询 因为一级缓存中已经存在了要查询的数据,所以就直接返回了,没有数据库查询操作
emps = mapper.queryEmp();
for (Emp emp : emps) {
System.out.println(emp+" : " + emp.getDept());
}
// 事务关闭 会清空一级缓存的数据 但不会清空二级缓存的数据
sqlSession.close();
// 重新开启事务
sqlSession = factory.openSession();
mapper = sqlSession.getMapper(EmpMapper.class);
// 因为是新开的事务,一级缓存中的数据是空的,但是二级缓存中有相关的数据 直接返回
emps = mapper.queryEmp();
for (Emp emp : emps) {
System.out.println(emp+" : " + emp.getDept());
}
sqlSession.close();
}
4.0.0
com.bobo
MyBatisDemo11Spring
1.0-SNAPSHOT
org.springframework
spring-context
5.1.6.RELEASE
org.springframework
spring-orm
5.1.6.RELEASE
org.mybatis
mybatis
3.4.6
mysql
mysql-connector-java
8.0.11
org.slf4j
slf4j-log4j12
1.7.25
org.mybatis
mybatis-spring
2.0.4
junit
junit
4.12
test
com.mchange
c3p0
0.9.5.2
src/main/java
**/*.xml
true
src/main/resources
*.xml
*.properties
true
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/shop?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username1=root
password=123456
#driver=com.mysql.cj.jdbc.Driver
#url=jdbc:mysql://192.168.100.122:3306/shop?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
#username=root
#password=123456
log4j.rootCategory=All, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\\logs\\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
package com.bobo.pojo;
public class User {
private Integer id;
private String username;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
'}';
}
}
package com.bobo.mapper;
import com.bobo.pojo.User;
import java.util.List;
public interface UserMapper {
List query();
}
package com.bobo.service;
import com.bobo.pojo.User;
import java.util.List;
public interface IUserService {
List query();
}
package com.bobo.service.impl;
import com.bobo.mapper.UserMapper;
import com.bobo.pojo.User;
import com.bobo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper mapper;
@Override
public List query() {
return mapper.query();
}
}
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService bean = ac.getBean(IUserService.class);
List list = bean.query();
for (User user : list) {
System.out.println(user);
}
}
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
package com.bobo.service.impl;
import com.bobo.mapper.EmpMapper;
import com.bobo.pojo.Emp;
import com.bobo.pojo.EmpExample;
import com.bobo.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements IEmpService {
@Autowired
private EmpMapper mapper;
@Override
public Emp queryEmpById(int id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public List query(Emp emp) {
EmpExample empExample = new EmpExample();
// 绑定查询的条件
EmpExample.Criteria criteria = empExample.createCriteria();
if(!"".equals(emp.getName()) && emp.getName() != null){
criteria.andNameLike("%"+emp.getName() +"%");
}
if(emp.getId() != null && emp.getId() > 0){
criteria.andIdEqualTo(emp.getId());
}
if(emp.getAge()!= null &&emp.getAge() > 0){
criteria.andAgeEqualTo(emp.getAge());
}
return mapper.selectByExample(empExample);
}
@Override
public Integer insert(Emp emp) {
return mapper.insertSelective(emp);
}
@Override
public Integer updateById(Emp emp) {
return mapper.updateByPrimaryKeySelective(emp);
}
@Override
public Integer deleteById(Emp emp) {
return mapper.deleteByPrimaryKey(emp.getId());
}
}