MybatisMybatisPlus参考⽂档⼀、实现简单增删改查
1 创建⼀个maven项⽬
2 导⼊依赖
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--log4j,视需求选择-->
声声慢歌词<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<!--在build中配置中resources,来防⽌我们资源导出失败的问题,防⽌target上不到⽂件-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
报告ppt模板下载免费完整版</resource>
</resources>radius认证过程
</build>
3 编写pojo实体类
User.java
package;
internalerror0x50dataset pytorchimport AllArgsConstructor;
import Data;
import NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructormysql语句的执行顺序
public class User {
private int id;
private String name;
private String pwd;
}
4 编写Dao接⼝和Mapper⽂件
4.1 UserMapper.java
package;
import User;
import List;
public interface UserMapper {
int addUser(User user);
int deleteUserById(int id);
int updateUser(User user);
User queryUserById(int id);
List<User>queryAllUser();
//通过注解写sql,就写下接⼝,l不⽤写了,
//本质:反射机制实现的,底层:动态代理
@Update("update mybatis.user set name = #{name},pwd =#{pwd} where id = #{id}") int updateUser2(User user);
@Select("select * from mybatis.user")
List<User>queryAllUser2();
}
4.l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lu.dao.UserMapper">
<insert id="addUser" parameterType="User">
insert into mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd})
</insert>
<delete id="deleteUserById" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
<update id="updateUser" parameterType="User">
update mybatis.user set name = #{name},pwd =#{pwd} where id = #{id}
</update>
<select id="queryUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
<select id="queryAllUser" resultType="User">
select * from mybatis.user
</select>
</mapper>
5 编写核⼼配置⽂件
5.1 db.properties
sql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8 username=root
password=123456
5.2 log4j.properties
## Global logging configuration 开发时候建议使⽤ debug
##
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
#控制台输出的相关设置
sole = org.apache.log4j.ConsoleAppender
sole.Target = System.out
sole.Threshold=DEBUG
sole.layout = org.apache.log4j.PatternLayout
#sole.layout.ConversionPattern=[%c]-%m%n
sole.layout.ConversionPattern=%5p [%t] - %m%n
#⽂件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/rzk.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#⽇志输出级别
batis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
5.l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<!--这边也可以写死,然后别的地⽅进⾏引⽤-->
<properties resource="db.properties"/>
<settings>
<!--标准⽇志⼯⼚的实现-->
<!--<setting name="logImpl" value="STDOUT_LOGGING"/>-->
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<!--这边可以整个包引⼊,也可以单个引⼊,注意顺序
实体类较少的情况下,可以⽤前⼀种,还可以⾃定义别名
实体类较多的情况下,可以使⽤后⼀种,默认别名为⾸字母⼩写的类名,⽐如user,如果需要改别名,可以在类上⾯通过注解@Alias("user")定义 -->
<!-- <typeAlias type="com.lu.pojo.User" alias="User"/>-->
<package name="com.lu.pojo"/>
</typeAliases>
<environments default="development">
<environment>
<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/lu/l"/>
</mappers>
</configuration>
6 编写⼯具类,获取SqlSession
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论