Spring和MyBaits整合
1、整合思路
2、整合环境
3、SqlSessionFactory
4、原始dao开发(和spring整合后)
package ecut.spring.dao;
import ecut.spring.po.Role;
public interface RoleDao {
//根据id获取角色
public Role getRole(Integer id) throws Exception ;
}
package ecut.spring.dao;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import ecut.spring.po.Role;
public class RoleDaoImpl extends SqlSessionDaoSupport implements RoleDao{
// SqlSessionDaoSupport有方法setSqlSessionFactory
@Override
public Role getRole(Integer id) throws Exception {
//继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSessoin
SqlSession session =this.getSqlSession();
//执行查询操作
Role role = session.selectOne("test.getRole", id);
// Spring 管理资源,使用完会自动关闭,释放资源
return role;
}
}
让RoleDaoImpl实现类继承SqlSessionDaoSupport,因为 SqlSessionDaoSupport有方法setSqlSessionFactory()方法,因此在applicationContext中使用提供setter方法进行注入,setter方法依赖于无参构造和setter方法,且在创建factory时候只需读取配置文件,因此在bean中需要配置configLocation属性。SqlSessionFactory通过Spring的配置文件注入,再由spring和mybatis整合自动完成SqlSessionFactory创建SqlSession。在接口实现类中通过this.getSqlSession()的方法获取到session完成操作。
在applicationContext.xml中配置dao。dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。
package ecut.spring.test;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ecut.spring.dao.RoleDao;
import ecut.spring.po.Role;
public class RoleDaoImplTest {
private AbstractApplicationContext container;
private RoleDao roleDao;
@Before
public void init() throws IOException {
// 指定configuration metadata配置元数据
String configLocations = "classpath:ecut/**/spring/applicationContext.xml";
// 创建spring IOC容器
container = new ClassPathXmlApplicationContext(configLocations);
roleDao = (RoleDao) container.getBean("roleDao", RoleDao.class);
}
@Test
public void testGetRole() throws Exception {
// 从容器中获取的bean实例中获取属性值
Role role = roleDao.getRole(1);
System.out.println(role);
}
@After
public void destroy(){
// 关闭spring的IOC容器
container.close();
}
}
5、mapper代理开发
package ecut.spring.mapper;
import ecut.spring.po.Role;
public interface RoleMapper {
//根据id获取角色
public Role getRole(Integer id) throws Exception ;
}
不整合之前是要通过session去创建RoleMapper对象,再由mybaits自动生成的mapper代理对象
SqlSession session = factory.openSession();
//创建RoleMapper对象,mybaits自动生成的mapper代理对象
RoleMapper roleMapper = session.getMapper(RoleMapper.class);
通过MapperFactoryBean创建代理对象存在的问题:需要针对每个mapper进行配置,麻烦。
若mapper 的名称是TRoleMapper自动扫描出来的mapper的bean的id为TRoleMapper而不是首字母小写tRoleMapper。可以根据日志来确定bean的id是多少,如RoleMapper的日志为DEBUG [main] - Creating shared instance of singleton bean 'roleMapper',则可以知道RoleMapper的id是roleMapper。
package ecut.spring.test;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ecut.spring.mapper.RoleMapper;
import ecut.spring.po.Role;
public class RoleMapperTest {
private AbstractApplicationContext container;
private RoleMapper roleMapper;
@Before
public void init() throws IOException {
// 指定configuration metadata配置元数据
String configLocations = "classpath:ecut/**/spring/applicationContext.xml";
// 创建spring IOC容器
container = new ClassPathXmlApplicationContext(configLocations);
roleMapper = (RoleMapper) container.getBean("roleMapper", RoleMapper.class);
}
@Test
public void testGetRole() throws Exception {
// 从容器中获取的bean实例中获取属性值
Role role = roleMapper.getRole(1);
System.out.println(role);
}
@After
public void destroy(){
// 关闭spring的IOC容器
container.close();
}
}
逆向工程
1、什么是逆向工程
2、环境配置
jar包下载地址: http://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core/1.3.7
3、使用方法
建议使用java程序方式,不依赖开发工具。
参照地址http://www.mybatis.org/generator/configreference/xmlconfig.html进行逆向工程配置文件配置
package ecut.reverse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSqlmap {
public void generator() throws Exception{
List warnings = new ArrayList();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("./src/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
参照地址http://www.mybatis.org/generator/configreference/xmlconfig.html,运行后生成代码如下
测试类
package ecut.reverse.test;
import java.io.IOException;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ecut.reverse.mapper.TRoleMapper;
import ecut.reverse.po.TRole;
import ecut.reverse.po.TRoleExample;
public class RoleTest {
private AbstractApplicationContext container;
private TRoleMapper roleMapper;
@Before
public void init() throws IOException {
// 指定configuration metadata配置元数据
String configLocations = "classpath:ecut/**/reverse/applicationContext.xml";
// 创建spring IOC容器
container = new ClassPathXmlApplicationContext(configLocations);
// 从容器中获取的bean实例中获取属性值
roleMapper = (TRoleMapper) container.getBean("TRoleMapper", TRoleMapper.class);
}
//根据主键删除
@Test
public void testDeleteByPrimaryKey() {
roleMapper.deleteByPrimaryKey(8);
}
//插入
@Test
public void testInsert() {
//构造 角色对象
TRole role = new TRole();
role.setRoleName("路飞");
role.setNote("海贼王");
role.setComicId(2);
roleMapper.insert(role);
}
//自定义条件查询
@Test
public void testSelectByExample() {
TRoleExample roleExample = new TRoleExample();
//通过criteria构造查询条件
TRoleExample.Criteria criteria = roleExample.createCriteria();
criteria.andComicIdEqualTo(2);
criteria.andIdGreaterThanOrEqualTo(2);
//可能返回多条记录
List list = roleMapper.selectByExample(roleExample);
for(TRole r:list){
System.out.println("id="+r.getId()+",roleName="+r.getRoleName()+",note="+r.getNote()+",comicId="+r.getComicId());
}
}
//根据主键查询
@Test
public void testSelectByPrimaryKey() {
TRole role = roleMapper.selectByPrimaryKey(1);
System.out.println("id="+role.getId()+",roleName="+role.getRoleName()+",note="+role.getNote()+",comicId="+role.getComicId());
}
//更新数据
@Test
public void testUpdateByPrimaryKey() {
//对所有字段进行更新,需要先查询出来再更新
TRole role = roleMapper.selectByPrimaryKey(8);
role.setComicId(3);;
//没有非空判断
roleMapper.updateByPrimaryKey(role);
//如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新
//roleMapper.updateByPrimaryKeySelective(record);
}
@After
public void destroy(){
// 关闭spring的IOC容器
container.close();
}
}
mapper 的名称是TRoleMapper自动扫描出来的mapper的bean的id为TRoleMapper而不是首字母小写tRoleMapper。可以根据日志来确定bean的id是多少,如TRoleMapper的日志为DEBUG [main] - Creating shared instance of singleton bean 'TRoleMapper',则可以知道TRoleMapper的id是TRoleMapper。
MyBaits视频及资料
视频:http://yun.itheima.com/course/289.html
资料:https://pan.baidu.com/s/1pKFaqTH 密码:w00x
转载请于明显处标明出处
https://www.cnblogs.com/AmyZheng/p/9385887.html