对象关系映射(Java中的对象 对应 数据库中的表)
mybatis
MyBatis-Plus (baomidou.com)官网
Java 持久层API
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.13version>
dependency>
2.3 MySQL driver
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.32version>
dependency>
note:因为此时为入门所以放在一块后续则不然
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql:/db(数据库名)
db.username=//自己的用户名
db.password=//密码
建议直接复制 需要注意 里面的映射关系需要修改
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com/itheima/mapper"/>
mappers>
configuration>
(1)导入依赖
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.20.0version>
dependency>
(2)配置log4j2.xml文件
note:这里我的包名为com 也就是src/main/java/com 如果是别的需要修改对应的名字下方会有标注
log4j2.xml文件
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
Console>
<File name="LogToFile" fileName="log/test.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
File>
Appenders>
<Loggers>
<Logger name="com" level="error">
<AppenderRef ref="LogToConsole"/>
Logger>
<Logger name="com" level="debug" additivity="false">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToFile"/>
Logger>
<Root level="error">
<AppenderRef ref="LogToConsole"/>
Root>
Loggers>
Configuration>
整体项目结构如图(log4j2.xml 为日志文件)
Xxmapper
public interface DbMapper {
String selectversion();
}
Xxmapper.xml
因为xml文件和mapper接口在java包下系统不会自动编译其下的xml文件需要pom.xml文件中添加buder
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
<include>**/*.htmlinclude>
includes>
resource>
resources>
build>
因为添加之后resources文件也有可能不会自动编译建议加上
是否编译可以看一下打包后的target/classes文件夹下是否有这个xml文件
在test目录下编写测试代码 这一种需要配置mybatis-conf.xml文件链接数据库
public class Demo {
public static void main(String[] args) {
try (InputStream is = Resources.getResourceAsStream("mybatis-conf.xml")) {
//建立会话工厂
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = build.openSession();
DbMapper mapper = sqlSession.getMapper(DbMapper.class);
Book book = new Book();
book.setPrice(10.0D);
book.setNumber(15);
book.setBname("《斗feng大陆》");
book.setCratedate(LocalDateTime.now());
System.out.println(mapper.add(book));
sqlSession.commit();
System.out.println(mapper.selectAll());
System.out.println(mapper.selectversion());
System.out.println(mapper.dbs());
} catch (Exception e) {
e.printStackTrace();
}
}
}
也可以直接在代码中链接数据库 但是大部分时间基本不用
public class Deno {
public static void main(String[] args) {
PooledDataSource ds = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql://localhost:3306/mybatis","root","");
TransactionFactory ts = new JdbcTransactionFactory();
Environment env = new Environment("dev",ts,ds);
Configuration cfg = new Configuration(env);
cfg.addMapper(DbMapper.class);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
SqlSession ss = sf.openSession();
DbMapper mapper = ss.getMapper(DbMapper.class);
System.out.println(mapper.selectversion());
System.out.println(mapper.dbs());
System.out.println(mapper.selectAll());
}
}
……………………………………day01…………………………………………