Mybatis映射文件

创建项目maven:

在这里插入图片描述
添加依赖:
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.sxt</groupId>
  <artifactId>mybatis-08</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis</artifactId>
  		<version>3.4.6</version>
  	</dependency>
  	<dependency>
  		<groupId>org.slf4j</groupId>
  		<artifactId>slf4j-log4j12</artifactId>
  		<version>1.7.25</version>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.27</version>
  	</dependency>
  </dependencies>
</project>

UsreDao接口:

package com.sxt.dao;

import org.apache.ibatis.annotations.Param;

import com.sxt.pojo.UserDto;

public interface UserDao {
	
	public int delete1(Integer id);
	
	public int delete2(@Param("id")Integer id);
	
	public int insert1(String username,String password);
	
	public int insert2(String username,String password);
	
	public int insert3(@Param("name") String username,@Param("password")String password);

	public int addUser(UserDto dto);
}

userDao.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.sxt.dao.UserDao">
 <!-- 预编译通过PreparedStatement方式实现的 -->
 <delete id="delete2" parameterType="int">
 	delete from t_user where id=${id}s
 </delete>
<!--  直接赋值通过Statement方式实现的而且接口中需要通过@Param接口指定key值 -->
 <delete id="delete1" parameterType="int">
 	delete from t_user where id=#{id}
 </delete>
 <delete id="insert1">
 	insert into t_user(username,password)values(#{arg0},#{arg1})
 </delete>
 <delete id="insert2">
 	insert into t_user(username,password)values(#{param1},#{param2})
 </delete>
 <delete id="insert3">
 	insert into t_user(username,password)values(#{name},#{password})
 </delete>
 <insert id="addUser" parameterType="UserDto">
 	insert into t_user(username,password,age,address)
 	values(#{user.username},#{password},#{age},#{address})
 </insert>
 </mapper> 

user类:

package com.sxt.pojo;

public class User {
	
	private Integer id;
	
	private String username;
	
	private String password;

	public Integer getId() {
		return id;
	}

	
	public User() {
		super();
	}


	public User(Integer id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}


	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}

UserDto:

package com.sxt.pojo;

public class UserDto {

	private User user;
	
	private int age;
	
	private int address;

	
	public UserDto() {
		super();
	}

	public UserDto(User user, int age, int address) {
		super();
		this.user = user;
		this.age = age;
		this.address = address;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAddress() {
		return address;
	}

	public void setAddress(int address) {
		this.address = address;
	}
	
	
}

test测试类:

package com.sxt.test;


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 com.sxt.dao.UserDao;
import com.sxt.pojo.User;
import com.sxt.pojo.UserDto;

public class Test {

	@org.junit.Test
	public void test1() throws IOException {
		
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		
		SqlSession session=factory.openSession();
		
		UserDao dao=session.getMapper(UserDao.class);
		
		dao.delete1(22);
		
		session.commit();
		
		session.close();
		
	}
	@org.junit.Test
	public void test2() throws IOException {
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		SqlSession session=factory.openSession();
		
		UserDao dao=session.getMapper(UserDao.class);
		dao.insert3("sdf", "ss");
		session.commit();
		session.close();
		
	}
	
	@org.junit.Test
	public void test3() throws IOException {
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		SqlSession session=factory.openSession();
		
		UserDao dao=session.getMapper(UserDao.class);
		User user=new User();
		user.setUsername("sdf");
		user.setPassword("uikl");
		UserDto dto=new UserDto();
		dto.setUser(user);
		dto.setAge(545);
		dto.setAddress(8998);
		session.commit();
		session.close();
		
	}
}

db.properties

#\\u5F00\u53D1\u73AF\u5883
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/easyui?characterEncoding=utf-8
username=root
password=yang

log4j.properties


log4j.rootCategory=DEBUG, stdout , R
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
 
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\\Tomcat 5.5\\logs\\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

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="db.properties"></properties> 
	
	<!-- 别名不区分大小写 -->
	<typeAliases>
		<!-- <typeAlias type="com.sxt.pojo.User"/> -->
		<!-- 指定类型名所属的包package -->
		<package name="com.sxt.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/sxt/dao/UserDao.xml"/>
		
	</mappers>
</configuration>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值