`
gouzhiguo
  • 浏览: 7837 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

mybatis 增加、删除、修改、查看(一对多、多对一映射)源代码

    博客分类:
  • java
阅读更多


 

 

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 50519
Source Host           : localhost:3306
Source Database       : mybatis

Target Server Type    : MYSQL
Target Server Version : 50519
File Encoding         : 65001

Date: 2014-02-20 14:25:02
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `customer`
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
  `sysNo` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) DEFAULT NULL,
  `userpass` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`sysNo`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of customer
-- ----------------------------
INSERT INTO `customer` VALUES ('1', 'immigrant', '000000');
INSERT INTO `customer` VALUES ('2', 'yiminghu', '000000');
INSERT INTO `customer` VALUES ('3', '王五', '000000');
INSERT INTO `customer` VALUES ('8', 'yiminghu', '000000');

-- ----------------------------
-- Table structure for `message`
-- ----------------------------
DROP TABLE IF EXISTS `message`;
CREATE TABLE `message` (
  `SysNo` int(11) NOT NULL AUTO_INCREMENT,
  `CustomerSysNo` int(11) DEFAULT NULL,
  `FullName` varchar(255) DEFAULT NULL,
  `Mobile` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`SysNo`),
  KEY `CustomerSysNo` (`CustomerSysNo`),
  CONSTRAINT `message_ibfk_1` FOREIGN KEY (`CustomerSysNo`) REFERENCES `customer` (`sysNo`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of message
-- ----------------------------
INSERT INTO `message` VALUES ('1', '1', '张三', '15015000000');
package com.model;

import java.util.List;

public class Customer {
	private Integer sysNo;
	private String  username;
	private String  userpass;
	private List<Message> messageList;

	public List<Message> getMessageList() {
		return messageList;
	}
	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}
	public Integer getSysNo() {
		return sysNo;
	}
	public void setSysNo(Integer sysNo) {
		this.sysNo = sysNo;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpass() {
		return userpass;
	}
	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}
}
package com.model;

public class Message {
	private Integer sysNo;
	private Integer customerSysNo;
	private String  fullName;
	private String  mobile;
	private Customer customer;
	
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Integer getSysNo() {
		return sysNo;
	}
	public void setSysNo(Integer sysNo) {
		this.sysNo = sysNo;
	}
	public Integer getCustomerSysNo() {
		return customerSysNo;
	}
	public void setCustomerSysNo(Integer customerSysNo) {
		this.customerSysNo = customerSysNo;
	}
	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
}

 

<?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.model.Customer">

	<select id="Get" parameterType="int" resultType="Customer">
		select * from customer where sysNo = #{sysNO}
	</select>
	
	<select id="seach" resultType="Customer">
		select * from customer
	</select>
	
	<insert id="Insert" parameterType="Customer">
		insert into Customer (username,userpass) value (#{username},#{userpass})
	</insert>
	
	<update id="update" parameterType="Customer">
		update customer set username=#{username},userpass=#{userpass} where sysNO=#{sysNo}
	</update>
	
	<delete id="delete" parameterType="int">
		delete from customer where sysNo=#{sysNo}
	</delete>
	<!-- 一对多 -->
	<resultMap type="Customer" id="resultMapCustomerleftJoinMessage">
		<id property="sysNo" column="sysNo" />
		<result property="username" column="username" />
		<result property="userpass" column="userpass" />
		<collection property="messageList" ofType="Message" column="CustomerSysNo"> 
			<id property="sysNo" column="sysNo" />
			<result property="fullName" column="fullName" />
			<result property="mobile" column="mobile" />
		</collection>
	</resultMap>
	
	<select id="Seach" parameterType="int" resultMap="resultMapCustomerleftJoinMessage">
		select c.*,m.fullName,m.mobile from customer c left join message m on c.sysno=m.customersysno where m.sysno = #{sysno}
	</select>
</mapper>
<?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.model.Message">

	<select id="Get" parameterType="int" resultType="Message">
		select * from Message where sysNo = #{sysNO}
	</select>
	
	<!-- 多对一 -->
	<resultMap type="Message" id="resultMapMessageleftJoinCustomer">
		<id property="sysNo" column="sysNo" />
		<result property="fullName" column="fullName" />
		<result property="mobile" column="mobile" />
		<association property="customer" javaType="Customer">
			<id property="sysNo" column="sysNo" />
			<result property="username" column="username" />
		</association>
	</resultMap>
	
	<select id="Seach" parameterType="int" resultMap="resultMapMessageleftJoinCustomer">
		select m.*,c.username from message m left join customer c on m.customersysno=c.sysno where m.sysno = #{sysno}
	</select>
</mapper>

  

 

package com.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;

public class MyBatisUtil {
	
	private static SqlSessionFactory factory;
	static {
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			factory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建
	 * @return
	 */
	public static SqlSession createSession() {
		return factory.openSession();
	}
	
	/**
	 * 关闭
	 * @param session
	 */
	public static void closeSession(SqlSession session) {
		if(session!=null){
			session.close();
		}			
	}
	
}

 

  • 大小: 18.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics