Maven构建MyBatis项目Demo
来源:互联网 发布:比亚迪和特斯拉知乎 编辑:程序博客网 时间:2024/06/06 02:59
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.Driverurl=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMTusername=rootpassword=
log4j.rootLogger=DEBUG,stdoutlog4j.logger.org.mybatis=DEBUGlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.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;}}
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();}}
阅读全文
1 0
- Maven构建MyBatis项目Demo
- Spring+SpringMVC+Mybatis(SSM)+Maven构建web项目(demo)步骤
- maven构建MyBatis项目
- maven 构建springmvc+mybatis项目
- Maven下构建bootstrap+jfinal项目Demo
- mybatis代码自动生成Demo(maven项目)
- Maven学习(八)- 构建MyBatis项目
- maven构建spring mvc项目 + Mybatis整合
- maven构建spring mvc项目 + Mybatis整合
- maven构建spring mvc项目 + Mybatis整合
- Maven学习(八)- 构建MyBatis项目
- springmvc+spring+mybatis+maven项目构建
- Maven学习(八)- 构建MyBatis项目
- 使用maven构建springmvc-mybatis项目
- Maven学习(八)- 构建MyBatis项目
- 构建项目Maven+SpringMVC+Spring+Mybatis
- Maven学习(八)- 构建MyBatis项目
- maven 构建Spring项目整个Mybatis
- 设计模式C++实现(2)——策略模式
- 如何使用工具调试linux服务程序
- <34>——Search for a Range
- VMware中Centos7.2的桥接模式的网络配置
- CSS3 3D Transform perspective 以及scale3d记录
- Maven构建MyBatis项目Demo
- Apache Spark 2.2.0 中文文档
- FTPrep, 82 Remove Duplicates from Sorted List II
- ES6之块级作用域分析
- java7 MethodHandle学习笔记
- 58数据库规约
- the transaction flow in v1.0 of Hyperledger Fabric(fabric1.0交易流程)
- CentOs 常用命令
- 深度、广度优先遍历树