Maven构建MyBatis项目Demo

1.项目的目录

Maven构建MyBatis项目Demo_第1张图片

2.pom.xml文件


	4.0.0

	com.jCuckoo
	MybatisTest
	0.0.1-SNAPSHOT
	jar

	MybatisTest
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			3.8.1
			test
		
		
		
			org.mybatis
			mybatis
			3.4.5
		
		
		
			mysql
			mysql-connector-java
			8.0.7-dmr
		
		
		
			log4j
			log4j
			1.2.17
		

	


3.jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username=root
password=


4.log4j.properties
log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c: %m%n

5.SqlSessionFactoryUtil.java
package com.jCuckoo.util;

import java.io.IOException;
import java.io.InputStream;

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 org.apache.log4j.Priority;

public class SqlSessionFactoryUtil {
	private static SqlSessionFactory sqlSessionFactory = null;
	private static final Class CLASS_LOCK=SqlSessionFactoryUtil.class;
	
	private SqlSessionFactoryUtil() {
		
	}
	public static SqlSessionFactory initSqlSessionFactory() {
		String resource = "mybatis_config.xml";
//		String resource = "mybatis_config1.xml";
//		String resource = "mybatis_config2.xml";
		InputStream inputStream = null;
		try {
			inputStream=Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			Logger.getLogger(SqlSessionFactoryUtil.class.getName()).log(Priority.DEBUG, null, e);
		}
		synchronized (CLASS_LOCK) {
			if(sqlSessionFactory==null) {
				sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
			}
		}
		return sqlSessionFactory;
	}
	public static SqlSession openSqlSession() {
		if(sqlSessionFactory==null) {
			 initSqlSessionFactory();
		}
		return sqlSessionFactory.openSession();
	}
	
}

6.Role.java

package com.jCuckoo.chapter02.pojo;

import org.apache.ibatis.type.Alias;

//@Alias("role")
public class Role {
	private Long id;
	private String roleName;
	private String note;
	
	
	public Role() {
		super();
	}
	public Role(String roleName, String note) {
		super();
		this.roleName = roleName;
		this.note = note;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	
}


7.RoleMapper.java

package com.jCuckoo.chapter02.mapper;

import com.jCuckoo.chapter02.pojo.Role;

public interface RoleMapper {
	public Role getRole(Long id);
	public int insertRole(Role role);
	public int deleteRole(Long id);
}

8.RoleMapper.xml




	
	
		insert into t_role(role_name,note) value(#{roleName},#{note})
	
	
		delete from t_role where id=#{id}
	

9.mybatis_config.xml




	
	
		
		
		
	
	
		
			
			
				
				
				
				
			
		
	
	
		
	
 

10.Chatper02Main测试类

package com.jCuckoo.chapter02.main;

import org.apache.ibatis.session.SqlSession;

import com.jCuckoo.chapter02.mapper.RoleMapper;
import com.jCuckoo.chapter02.pojo.Role;
import com.jCuckoo.util.SqlSessionFactoryUtil;

public class Chatper02Main {

	public static void main(String[] args) {
		SqlSession sqlSession=null;
		sqlSession =SqlSessionFactoryUtil.openSqlSession();
		RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class);
		/*Role role=new Role("testName","testNote");
		roleMapper.insertRole(role);*/
		//roleMapper.deleteRole((long) 1);
		
		Role role=roleMapper.getRole((long)2);
		System.out.println(role.getId()+" "+role.getRoleName()+" "+role.getNote());
		sqlSession.commit();
	}

}






你可能感兴趣的:(J2EE,MyBatis)