MybatisPlus基本增删改查和条件构造器删改查数据库:
依赖:
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId&hange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
相关配置⽂件:
c语言程序设计第5版pdf<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration >
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd
HH:mm:ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
db.properties:
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://x:3306/mp?useUnicode=true&characterEncoding=UTF8 jdbc.username=root
jdbc.password=123456
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:tx="/schema/tx"
xmlns:mybatis-spring="/schema/mybatis-spring"
xsi:schemaLocation="/schema/mybatis-spring
/schema/mybatis-spring-1.2.xsd
/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context-4.0.xsd
/schema/tx
/schema/tx/spring-tx-4.0.xsd">
<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource"
class="hange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事务管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基于注解的事务管理 -->
<tx:annotation-driven
transaction-manager="dataSourceTransactionManager"/>
<!-- 配置 SqlSessionFactoryBean
mybatis提供的:batis.spring.SqlSessionFactoryBean
mybatisplus提供的:batisplus.spring.MybatisSqlSessionFactoryBean
-->
<bean id="sqlSessionFactoryBean"
class="batisplus.spring.MybatisSqlSessionFactoryBean"> <!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value="l"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage"
value="ity"></property>
<!--注⼊全局mybatisplus策略配置-->
<property name="globalConfig" ref="globalConfiguration"></property>
</bean>
<!--
配置 mybatis 扫描 mapper 接⼝的路径
-->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="batisplus.mapper"></property>
</bean>
<!--定义mybatisplus的全局策略配置 -->
<bean id="globalConfiguration" class="ity.GlobalConfiguration"> <!--实体类下划线转驼峰-->
<property name="dbColumnUnderline" value="true"></property>
<!--配置全局id⾃增长策略-->
<property name="idType" value="0"></property>
<!--全局的表前缀配置-->
<property name="tablePrefix" value="tbl_"></property>
</bean>
</beans>
entity:
ity;
import batisplus.annotations.TableField;
import batisplus.annotations.TableId;
import batisplus.annotations.TableName;
import ums.IdType;
/**
* javaBean
* 定义javaBean中成员变量时所使⽤的类型
*/
/**
* @TableName:
* value:数据库表名
*
*/
@TableName(value ="tbl_employee" )
public class Employee {
/*id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1),
age INT*/
/
**
* @TableId:
*  value:指定表中的主键列的列名,如果实体属性名与列名⼀致可以省略    *  type:指定主键策略
*
*/
@TableId(value = "id",type = IdType.AUTO)
@TableField( value = "id",exist = true)
private Integer id;
@TableField(value = "last_name",exist = true)
private String lastName;
@TableField(value = "email",exist = true)
private String email;
@TableField(value = "gender",exist =true)
private Integer gender;
@TableField(value ="age",exist = true)
private  Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
mysql面试题 增删改查@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", age=" + age +
'}';
}
}
Mapper:
kj.mybatisplus.mapper;
import batisplus.mapper.BaseMapper;
ity.Employee;
/**
* mapper接⼝
* :基于mybatis:在Mapper接⼝中编写CRUD相关⽅法,提供Mapper接⼝所对应的SQL映射⽂件⼀级⽅法对应的SQL语句 * :基于mybatisplus:让xxxMapper接⼝继承baseMapper<T>即可,T指定的就是当前mapper接⼝所操作的实体类类型
*/
public interface EmployeeMapper  extends BaseMapper<Employee> {
}
Test:
基本增删改查
st;
import batisplus.plugins.Page;
ity.Employee;
kj.mybatisplus.mapper.EmployeeMapper;
import org.apache.ibatis.session.RowBounds;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestMybatisPlus {
private ApplicationContext ioc=new ClassPathXmlApplicationContext("l");
@Test
public void testDataSource() throws SQLException {
DataSource Bean("dataSource",DataSource.class);
System.out.println(dataSource);
Connection connection = Connection();
System.out.println(connection);
}
/**
* 从容器中获取employeeMapper
*/
private EmployeeMapper Bean("employeeMapper",EmployeeMapper.class);
/**
* 通⽤插⼊操作插⼊实体类设置值的字段,只有⾮空的属性对应的字段才会出现到SQL语句中
* INSERT INTO tbl_employee ( last_name, age ) VALUES ( ?, ? )
*/
@Test
public void testInsert(){
Employee employee=new Employee();
employee.setLastName("zhechaochao");
employee.setAge(18);
Integer row = employeeMapper.insert(employee);
System.out.println("row="+row);
/
/获取数据库中的主键值
Integer id = Id();
System.out.println(id);
}
幂函数的图像和性质/**
* 通⽤插⼊操作插⼊时不管属性是否⾮空,属性所对应的字段都会出现到SQL语句中
* INSERT INTO tbl_employee ( last_name,email,gender,age ) VALUES ( ?,?,?,? )
*/
@Test
public void testInsertAllColumn(){
Employee employee=new Employee();
employee.setLastName("zhechaochao");
employee.setAge(18);
Integer row = employeeMapper.insertAllColumn(employee);
System.out.println("row="+row);
//获取数据库中的主键值
Integer id = Id();
System.out.println(id);
}
/**
* 根据id更新
* UPDATE tbl_employee SET gender=?, age=? WHERE id=?
*/
@Test
public void testUpdateById(){
Employee employee=new Employee();
employee.setId(7);
employee.setGender(1);
employee.setAge(25);
Integer row = employeeMapper.updateById(employee);
System.out.println("row="+row);
}
/**
* 根据id更新
* 慎⽤,更新所有字段其他字段原本有值会被设置为null
* UPDATE tbl_employee SET last_name=?,email=?,gender=?,age=? WHERE id=?
*/
@Test
public void testUpdateAllColumnById(){
Employee employee=new Employee();
employee.setId(7);
employee.setGender(1);
employee.setAge(25);
js转jquery对象Integer row = employeeMapper.updateAllColumnById(employee);
System.out.println("row="+row);
}
/**
* 更具id查询
* SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE id=?
*/
@Test
public void testSelectById(){
Employee employee = employeeMapper.selectById(1);
System.out.println(employee);
}
/
什么原因引起类风湿性关节炎**
*通过多列进⾏查询 id+lastName
* SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE id=? AND last_name=?    */
@Test
public void testSelectOne(){
电动机的基本控制Employee employee=new Employee();
employee.setId(6);
employee.setLastName("zhechaochao");
Employee employee1 = employeeMapper.selectOne(employee);
System.out.println(employee1);
}
/**
*根据id批量查询
* SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE id IN ( ? , ? , ? )
*/
@Test

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