Maven构建MyBatis项目Demo

1.项目的目录


2.pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jCuckoo</groupId>
	<artifactId>MybatisTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>MybatisTest</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.5</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.7-dmr</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

	</dependencies>
</project>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jCuckoo.chapter02.mapper.RoleMapper">
	<select id="getRole" parameterType="long" resultType="role">
		select id, role_name as roleName, note from t_role where id = #{id}
	</select>
	<insert id="insertRole" parameterType="role">
		insert into t_role(role_name,note) value(#{roleName},#{note})
	</insert>
	<delete id="deleteRole" parameterType="long">
		delete from t_role where id=#{id}
	</delete>
</mapper>

9.mybatis_config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC 
	"-//mybatis.org//DTD Config 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties resource="jdbc.properties"></properties>
	<typeAliases>
		<typeAlias alias="role" type="com.jCuckoo.chapter02.pojo.Role" />
		<!-- 使用自动扫描的形式来定义别名,在Role中使用@Alias("role")注解进行说明-->
		<!-- <package name="com.jCuckoo.chapter02.pojo"/> -->
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com\jCuckoo\chapter02\mapper\RoleMapper.xml" />
	</mappers>
</configuration> 

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();
	}

}






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值