MyBatis代码笔记--0.0

MyBatis-01基于使用

一、入门实例(普通Java工程):完成简单的ORM映射

0.1环境搭建:

  • 数据库建库建表:--mysql5.5   --mybatisdb库  --user表   --若干字段
  • Java工程环境搭建:JDK1.8;JUnit4;
  • 其他依赖

MyBatis代码笔记--0.0_第1张图片

1.写一个pojo:User.java,此可理解为ORM--O

public class User {
	
	private int id;//id
	private String name;//用户名
	private String addr;//地址
	private int age;//年龄
    /** 省略setter and getter */
    /** 省略全字段toString() */
}

2.写个UserMapper.xml,此可以视为ORM--M

引入DTD约束



     

	
	

3.引入log4j.properties

# Global logging configuration
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

4.写一个测试类TestMyBatis.java

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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 cn.tedu.pojo.User;

/**
 *测试mybatis入门案例 
 *1.创建会话工厂
 *2.创建sqlsession,执行sql
 *3.处理结果
 *4.释放资源
 */
public class TestMybatis {
	//单元测试
	@Test
	public void hello() throws IOException {
		//1.创建会话工厂
		InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
		//2.创建sqlsession,执行sql
		SqlSession session = ssf.openSession();
		//定位sql的位置namespace值+id值
		List list = session.selectList("userns.all");
		//3.处理结果
		for (User user : list) {
			System.out.println(user);
		}
		//4.释放资源
		session.close();
	}
}

 

二、入门实例(maven项目):完成简单的ORM映射<增加一个pom.xml即可>

1.maven项目的pom.xml配置


	4.0.0
	cn.tedu
	mybatisday01V2.0
	0.0.1-SNAPSHOT
	
		
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.5.1
				
					1.8
					1.8
					UTF-8
				
			
		
	

	
		UTF-8
	
	
		
		
			mysql
			mysql-connector-java
			5.1.40
		
		
			junit
			junit
			4.11
		
		
			org.mybatis
			mybatis
			3.2.8
		
		
			log4j
			log4j
			1.2.17
		
	

---------------------------------------------------------------------------------------分割线--------------------------------------------------------------------------------------

三、MyBatis之Dao和*Mapper.xml

1.写一个User类<同上>和UserMapper.xml




	
	
		id,name,addr,age
	
	
	
	

	
		update user
		age=#{age}
		
			id=#{id}
		
	

	

	
		delete from user where id in
		#{o}
	

	
		update user set age=18 where id in(
		
		
			#{i}
		
		)
	

 

2.写一个Dao接口:UserMapperDao.java和InterfaceMapper.xml

UserMapperDao.java

public abstract interface UserMapperDao {
	public abstract List findAll();
	int count();
	User findOne();
	User findOne2(int id);
	int save(User user);
	int update(User user);
	int del(String str);
}

InterfaceMapper.xml





	
	
	
	
	
	
		insert into user values(null,#{name},#{addr},#{age})
	
	
		update user set age=#{age} where name=#{name}
	
	
		delete from user where name=#{name}
	

3.写一个database.properties,sqlMapConfig.xml

database.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatisdb
username=root
password=root

sqlMapConfig.xml




	
	
	
	
		
	
	
	
		
		
			
			
			
			
				
				
				
				
				
			
		
	
	
	
		
		
	

4.测试类DynamicSqlTest.java和InterfaceTest.java

DynamicSqlTest.java

/**
 * 动态sql的测试
 */
public class DynamicSqlTest {
	SqlSessionFactory ssf;//线程安全
	@Before
	public void init() {
		try {
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			ssf = new SqlSessionFactoryBuilder().build(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void sqlInclude() {
		SqlSession session = ssf.openSession();
		User user = session.selectOne("usernp.findOne",2);
		System.out.println(user);
		session.close();
	}
	@Test
	public void ifsql() {
		SqlSession session = ssf.openSession();
		User user = new User();
		user.setId(1);
		//user.setName("guangtouqiang");
		user.setAddr("北京");
		User select = session.selectOne("usernp.ifsql",user);
		System.out.println("这个select "+select);
		session.close();
	}
	@Test
	public void setsql() {
		SqlSession session = ssf.openSession(true);
		User user = new User();
		user.setAge(1000);
		user.setId(2);
		int update = session.update("usernp.setsql",user);
		System.out.println("修改了"+update+"条");
		session.close();
	}
	@Test
	public void foreacharray() {
		SqlSession session = ssf.openSession();
		List list = session.selectList("usernp.foreacharray",new int[] {1,2,3} );
		for (User user : list) {
			System.out.println(user);
		}
		session.close();
	}
	@Test
	public void foreachlist() {
		SqlSession session = ssf.openSession(true);
		List list = new ArrayList<>();
		list.add(1);
		list.add(2);
		session.delete("usernp.foreachlist",list);
		session.close();
	}
	@Test
	public void foreachupdate() {
		SqlSession session = ssf.openSession(true);
		Map map = new HashMap<>();
		map.put("key", new int[] {1,2,3});
		session.update("usernp.foreachupdate",map);
		session.close();
	}
}

InterfaceTest.java

/**
 * 测试mapper接口 
 */
public class InterfaceTest {
	SqlSessionFactory ssf;
	@Before
	public void init() {
		try {
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			ssf = new SqlSessionFactoryBuilder().build(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void all() {
		//1.获取sqlsession,执行sql
		SqlSession session = ssf.openSession();
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		List list = dao.findAll();
		for (User user : list) {
			System.out.println(user);
		}
		session.close();
	}
	@Test
	public void count() {
		SqlSession session = ssf.openSession();
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		int count = dao.count();
		System.out.println(count);
		session.close();
	}
	@Test
	public void findOne() {
		SqlSession session = ssf.openSession();
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		User user = dao.findOne();
		System.out.println(user);
		session.close();
	}
	@Test
	public void findOne2() {
		SqlSession session = ssf.openSession();
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		User user = dao.findOne2(2);
		System.out.println(user);
		session.close();
	}
	@Test
	public void save() {
		SqlSession session = ssf.openSession(true);
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		User user = new User();
		user.setName("预知未来");
		user.setAddr("在今天");
		user.setAge(18);
		int save = dao.save(user);
		System.out.println(save);
		session.close();
	}
	@Test
	public void update() {
		SqlSession session = ssf.openSession(true);
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		User user = new User();
		user.setAge(26);
		user.setName("念你");
		int update = dao.update(user);
		System.out.println(update);
		session.close();
	}
	@Test
	public void del() {
		SqlSession session = ssf.openSession(true);
		UserMapperDao dao = session.getMapper(UserMapperDao.class);
		String str = "ok";
		int del = dao.del(str);
		System.out.println(del);
		session.close();
	}
}

四、多表联查和一二级缓存

UserInfoMapper.xml




	
	
	
	
	
		
		
		
		
		
	
	
	
		
		
			
			
			
			
		
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	

sqlMapConfig.xml




	
	
	
	
		
	
	
	
		
	
	
	
		
		
			
			
			
			
				
				
				
				
				
			
		
	
	
	
		
	

 

 

 

 

 

 

你可能感兴趣的:(Mybatis相关)