标签:last name gender 改查 IDEA public Maven id String
一:IDEA使用Maven搭建MyBatis
1.新建一个文件夹,IDEA中的project其实就相当于eclipse中的工作空间(工作文件夹),改成自己想要的名字,并打开这个文件夹
File→Open→文件夹→OK
2.右键这个项目,新建一个Module,在IDEA中Module就相当于eclipse中的project
二:在pom.xml中导入以下代码
导入后maven会自动下载这些jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<finalName>ssm</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3.建表
4.①编写MyBatis的Xml配置文件,②编写实体类, ③dao层接口,④和EmpMapper.xml文件中的SQL语句
MyBatis-config.xml配置文件中写入以下代码
注意:更改自己的数据库帐号密码还有连接的数据库的名字
<?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 default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- mapping文件路径配置 -->
<mappers>
<mapper resource="mapper/EmpMapper.xml"/>
</mappers>
</configuration>
实体类:
/**
* @author ipllt
* @create 2018-12-06 下午 1:05
*/
public class Emp {
private int id;
private String last_name;
private String gender;
private String email;
public String toString() {
return "Emp{" +
"id=" + id +
", last_name='" + last_name + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
public Emp() {
}
public Emp(int id, String last_name, String gender, String email) {
this.id = id;
this.last_name = last_name;
this.gender = gender;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
dao层
public interface EmpMapper {
/**mybatis中与empxml文件绑定的接口
* 按照ID查员工的方法*/
Emp getEmpById(int id);
//添加员工
void addEmp(Emp emp);
//更新员工
void updateEmp(Emp emp);
//通过ID删除员工
void deleteEmpById(int id);
}
EmpMapper.xml
<?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="com.pl.dao.EmpMapper">
<!-- 将ID改为接口中的方法名-->
<select id="getEmpById" parameterType="int" resultType="com.pl.pojo.Emp">
select * from tbl_employee where id = #{id}
</select>
<insert id="addEmp" parameterType="com.pl.pojo.Emp">
insert into tbl_employee(last_name,gender,email)
value (#{last_name},#{gender},#{email})
</insert>
<update id="updateEmp">
update tbl_employee
set last_name=#{last_name},gender=#{gender},email=#{email}
where id=#{id}
</update>
<!-- 删除-->
<delete id="deleteEmpById">
delete from tbl_employee where id=#{id}
</delete>
</mapper>
5.在测试类中新建测试类,并运行
public class EmpTest {
@Test
public void empFindByIdTest(){
//定义读取文件名
String resources = "mybatis-config.xml";
//创建流
Reader reader = null;
try {
//读取mybatis config.xml到reader对象中
reader = Resources.getResourceAsReader(resources);
}catch (IOException e){
e.printStackTrace();
}
//初始化mybatis,创建SqlSessionFactory
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
//创建Session实例
SqlSession session = sqlMapper.openSession();
//传入参数,返回结果
Emp emp = session.selectOne("com.pl.pojo.Emp.findById",2);
System.out.println(emp.toString());
//关闭session
session.close();
}
public SqlSessionFactory getSqlSessionFactory()throws IOException{
String resources = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resources);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test02() throws IOException{
//获取sqlsessionFactory
SqlSessionFactory sqlSessionFactory= getSqlSessionFactory();
//获取sqlsession对象
SqlSession openSession = sqlSessionFactory.openSession();
try {
//获取接口的实现类对象
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
Emp emp = mapper.getEmpById(3);
System.out.println(emp.toString());
}finally {
openSession.close();
}
}
@Test
public void test03()throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
//添加
// Emp emp = new Emp(6,"hahhh","1","666@qq.com");
// mapper.addEmp(emp);
//修改
// Emp emp = new Emp(1,"tom","1","888@qq.com");
// mapper.updateEmp(emp);
//删除
mapper.deleteEmpById(4);
openSession.commit();
}finally {
openSession.close();
}
}
}
标签:last,name,gender,改查,IDEA,public,Maven,id,String 来源: https://blog.csdn.net/qq_45626418/article/details/120384295
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。