MyBatis注解⽅式增删查改及实现动态SQL的条件查询,批量删除插⼊MyBatis的注解开发
MyBatis提供了下⾯注解进⾏映射⽂件配置
初中化学常量元素和微量元素口诀@Select 查询数据注解
@Insert 插⼊数据注解
@Delete 删除数据注解
@Update 修改数据注解
@Options 选项配置
@Results ⼿动映射配置
@Result : @results中的具体的某⼀列的映射信息配置
主配置⽂件 l
<!-- xml 约束,约束下⾯可以使⽤那些标签(元素)-->
<?xml version="1.0" encoding="UTF-8"?><!-- xml声明,固定语法-->
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<!--
开启Mybatis的配置
<configuration>根元素,任何html⽂件都有⼀个根元素
-->
<configuration>
<!--读取数据库配置⽂件-->
<properties resource="db.properties"/>
<!--设置-->
<settings>
<!--开启⽀持驼峰命名法-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置别名-->
<typeAliases>
<!--配置单个别名-->
<typeAlias type="batis.pojo.User" alias="User"/>
<!--使⽤包扫描配置别名,包下⾯所有pojo类都回设置别名,简单类名⾸字母变⼩写(不区分⼤⼩写)-->
<package name="batis.pojo"/>
</typeAliases>
<!--
配置Mybatis的环境
<environments default="mysql">
default:默认的环境,值就是内部<environment id="mysql">的id值
-->
<environments default="mysql">
<!--
具体环境标识
<environment id="mysql">
id:当前环境唯⼀标识见名知意
-
源代码国语 下载
->
<environment id="mysql">
<!--事务管理器(操作数据库有事务概念),使⽤默认JDBC (MyBatis底层就是封装原⽣JDBC)事务-->
<transactionManager type="JDBC"/>
<!--配置数据域(连接池)
<dataSource type="POOLED">
type:数据域类型POOLED,MyBatis内置的⼀个数据域
使⽤框架开发连接数据全部都是使⽤数据源连接,使⽤阿⾥巴巴连接池,Apache的DBCP连接池(和Spring框架集成详细配置)-->
<dataSource type="POOLED">
<!--数据库驱动-->
<property name="driver" value="${driverClassName}"/>
<!--连接数据库url地址-->
<property name="url" value="${url}"/>
<!--数据库账号-->
<property name="username" value="${username}"/>
<!--数据库密码-->
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--配置MyBatis的映射⽂件-->
<mappers>
<!--配置映射接⼝-->
<!--<mapper class="接⼝的全限定名"/>-->
<mapper class="batis.mapper.UserMapper"/>
</mappers>
</configuration>
获取Sql⼯⼚⼯具
package com.ywq.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.Null;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
public static SqlSessionFactory sessionFactory = null;
static{
try{
InputStream is = ResourceAsStream("l"); sessionFactory =new SqlSessionFactoryBuilder().build(is);
}catch(IOException e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sessionFactory.openSession();
}
public static void colose(SqlSession sqlSession){
if(sqlSession != null){
sqlSession.close();
}
}
}
pojo类代码
public class User {
private Integer id ;
private String name;
private String password;
private Integer age;
public Integer getId(){
return id;
}
public void setId(Integer id){联系我网页
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age = age;
网页登录界面的源代码}
public User(Integer id, String name, String password, Integer age){
super();
this.id = id;
this.name = name;
mysql菜鸟教程增删改查
this.password = password;
this.age = age;
}
public User(){
super();
}
@Override
public String toString(){
return"User [id="+ id +", name="+ name +", password="+ password +", age="+ age +"]"; }
}
Mapper接⼝代码
import java.util.List;
import javax.annotation.Resource;
import javax.annotation.Resources;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import batis.pojo.User;
/*sqlserver2008和r2区别
*mybatis的映射接⼝
* */
public interface UserMapper {
// DML :增删改
@Insert("insert into user (name,password,age)values(#{name},#{password},#{age})")
int insert(User user);
@Delete("delete from user where id = #{id}")
int deleteByPrimaryKey(Integer id);
@Update("update user set name = #{name},password=#{password},age=#{age} where id =#{id}") int updateByPrimaryKey(User user);
//DQL : 查询
@Select("select * from user where id = #{id}")
User selectByPrimaryKey(Integer id);
@Results({
@Result(id =true,column ="u_id",property ="id"),
@Result(column ="u_name",property ="name"),
@Result(column ="u_password",property ="password"),
@Result(column ="u_age",property ="age")
})
@Select("select id u_id,name u_name,password u_password, age u_age from user")
List<User>selectLits();
}
测试代码
package st;
import static org.junit.Assert.*;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import batis.mapper.UserMapper;
import batis.pojo.User;
import batis.util.MyBatisUtils;
public class MyBatisTest {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。