一、Maven
1.Maven是什么
Maven是专门用于管理和构建Java项目的工具,它的主要功能有:
2.Maven基本命令
mvn compile :编译
mvn clean:清理
mvn test:测试
mvn package:打包
mvn install:安装
3.在idea中创建和导入Maven项目
创建:文件->创建模块->maven->next
4.关于Maven项目编译、打包发生如下报错:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:co
原因:maven会自动把jdk版本改成1.5
永久解决方法:
在maven的conf文件夹中的settings.xml中添加如下代码(我用的是jdk11版本,如果是其他版本,如jdk8,只需要把11改成1.8):
<profile>
<id>jdk-11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
</profile>
5.Maven项目中通过设置pom.xml坐标导入jar包。
必须要有dependencies和dependency标签
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
重点:写完记得刷新,idea2020版本后无法启动自动刷新功能
二、Mybatis快速入门
1.Mybatis是什么
什么是持久层:
2.Mybatis代码编写流程
以下中的文件代码均可通过在Mybatis官网找到模板后修改
Mybatis官网
<?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>
<!--
environments:配置数据库连接环境信息。可以配置多个environment,通过default属性切换不同的
environment
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
//连接信息,包括数据库url,数据库name、password
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- sql 映射文件:存放编写好的sql语句-->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
<?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="test">
<select id="selectAll" resultType="indi.wwz.pojo.User">
select * from tb_User;
</select>
</mapper>
官方模板:
注意:如果UserMapper.xml配置文件中报黄(红),可以通过连接数据库解决:
package indi.wwz.pojo;
public class User {
private Integer id ;
private String username ;
private String password ;
private String gender ;
private String addr ;
public Integer getId() {
return id;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
public class MyBatisDemo {
public static void main(String[] args) throws IOException {
//1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象,用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 执行sql
//这里有一个问题:把sql的id写死,后面不方便维护和修改。后面使用mapper代理替代
List<User> users = sqlSession.selectList("test.selectAll"); //参数是一个字符串,该字符串必须是映射配置文件的namespace.id,通过参数区别不同文件中相同id的sql语句
System.out.println(users);
//4. 释放资源
sqlSession.close();
}
}