Java + myBatis + MySql
来源:互联网 发布:ubuntu jdk下载 编辑:程序博客网 时间:2024/06/05 20:46
Connecting to MySQL from Java application using myBatis.
Objective
- How to connect to MySQL database from Java application using myBatis?
- How to use MyBatis config & mapper using XML?
Environment
Eclipse (Indigo)
Libraries
-mySql java connector (jar)
- myBatis 3.1.1
( 1 ) Database
( 2 ) Java Project
- Create new Java Project
- Add new folder “lib”
- Copy mysql-connector-java (jar) and myBatis to “lib” folder
- Add jars file to java build path
( 3 ) Java Classes
Person.javaPersonDAO.javaMyBatisConnectionFactory.javaMain.java
Person.java
package com.hmkcode.vo;public class Person { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "id: "+id+" Name: "+name; }}
PersonDAO.java
package com.hmkcode.dao;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import com.hmkcode.vo.Person;public class PersonDAO { private SqlSessionFactory sqlSessionFactory = null; public PersonDAO(SqlSessionFactory sqlSessionFactory){ this.sqlSessionFactory = sqlSessionFactory; } /** * Returns the list of all Person instances from the database. * @return the list of all Person instances from the database. */ @SuppressWarnings("unchecked") public List<Person> selectAll(){ List<Person> list = null; SqlSession session = sqlSessionFactory.openSession(); try { list = session.selectList("Person.selectAll"); } finally { session.close(); } System.out.println("selectAll() --> "+list); return list; } /** * Select instance of Person from the database. * @param person the instance to be persisted. */ public Person selectById(int id){ Person person = null; SqlSession session = sqlSessionFactory.openSession(); try { person = session.selectOne("Person.selectById", id); } finally { session.close(); } System.out.println("selectById("+id+") --> "+person); return person; } /** * Insert an instance of Person into the database. * @param person the instance to be persisted. */ public int insert(Person person){ int id = -1; SqlSession session = sqlSessionFactory.openSession(); try { id = session.insert("Person.insert", person); } finally { session.commit(); session.close(); } System.out.println("insert("+person+") --> "+person.getId()); return id; } /** * Update an instance of Person into the database. * @param person the instance to be persisted. */ public void update(Person person){ int id = -1; SqlSession session = sqlSessionFactory.openSession(); try { id = session.update("Person.update", person); } finally { session.commit(); session.close(); } System.out.println("update("+person+") --> updated"); } /** * Delete an instance of Person from the database. * @param id value of the instance to be deleted. */ public void delete(int id){ SqlSession session = sqlSessionFactory.openSession(); try { session.delete("Person.delete", id); } finally { session.commit(); session.close(); } System.out.println("delete("+id+")"); }}
MyBatisConnectionFactory.java
package com.hmkcode.mybatis;import java.io.FileNotFoundException;import java.io.IOException;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisConnectionFactory { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "com/hmkcode/mybatis/config.xml"; Reader reader = Resources.getResourceAsReader(resource); if (sqlSessionFactory == null) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); } catch (IOException iOException) { iOException.printStackTrace(); } } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; }}
Main.java
package com.hmkcode;import java.util.List;import com.hmkcode.mybatis.MyBatisConnectionFactory;import com.hmkcode.dao.PersonDAO;import com.hmkcode.vo.Person;public class Main { public static void main(String args[]){ // Laod spring-config.xml file //ApplicationContext ctx = new ClassPathXmlApplicationContext("com/hmkcode/config/spring-config.xml"); //get jdbcTemplatePersonDAO PersonDAO personDAO = new PersonDAO(MyBatisConnectionFactory.getSqlSessionFactory()); //create person bean to insert Person person = new Person(); person.setName("Person 1"); //( 1 ) insert person personDAO.insert(person); //**set name of person person.setName("Person 2"); //** insert another person int id = personDAO.insert(person); //( 2 ) select persons by id personDAO.selectById(id); //( 3 ) select all List<Person> persons = personDAO.selectAll(); //**set name of all persons for(int i = 0; i < persons.size(); i++){ persons.get(i).setName("Person Name "+i); //( 4 ) update person personDAO.update(persons.get(i)); } //**check update persons = personDAO.selectAll(); }}
( 4 ) XML files
- config.xml
- Person.xml
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> <typeAliases> <typeAlias alias="Person" type="com.hmkcode.vo.Person"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!-- connecting to Local MySql --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/hmkcode"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/hmkcode/mybatis/mapper/Person.xml"/> </mappers></configuration>###Person.xml
( 5 ) Test Application
0 0
- Java + myBatis + MySql
- 关键字:mysql,mybatis,java日期
- java-mysql-mybatis日期类型
- 后台 问题集合(mysql+mybatis+java)
- java通过mybatis查询mysql服务
- java+mybatis+mysql 中文乱码问题
- Mybatis Mysql
- mybatis框架下java连接mysql,汉字乱码显示为“???”
- play2+guice+mybatis+ehcache+mysql范例(java版)
- java后台框架源码 springmvc mybatis oracle mysql maven
- java后台框架 springmvc mybatis oracle 和 mysql数据库
- java后台框架 springmvc mybatis(oracle 和 mysql数据库)
- java后台框架 springmvc mybatis(oracle 和 mysql数据库)
- java后台框架源码 springmvc mybatis oracle mysql maven
- mysql 表结构生成java实体与mybatis映射
- java后台框架源码 springmvc mybatis oracle mysql maven
- java后台框架源码 springmvc mybatis oracle mysql maven
- java-mybatis generator-生成 mysql 数据库访问文件
- Constructor injection type ambiguities in Spring
- iOS-Http : GET : POST
- C# 之屏幕找图
- Spring事务的隔离级别
- HashMap的key可以是可变的对象吗???
- Java + myBatis + MySql
- 程序上面,一般不使用 悲观锁
- Rearrange int array to put odd number to left and even to right
- iOS疯狂详解之SQlite数据库增删改查
- Citirx reciever 客户端添加帐户
- LeetCode-Reverse Linked List
- SWERC2014 PRO A
- 深度学习word2vec笔记之算法篇
- *LeetCode-Happy Number