这段时间一直都在忙着考研,也没时间学专业的内容,整天的高数和英语,今天是学校安排实习的第二天,在实习的过程中我终于有重新敲起了代码,感觉真爽哈哈。下面来分享一下今天实习的学习成果。
1.首先就是IDEA中进行MySQL-connect驱动的导入这里如果有不会的可以去看我的这篇文章,这里就不在多说了。
2.然后就是IDEA与数据库的连接,这里也就不再多说了,不会的也可以看上篇文章。
3.其次就是maven环境的配置,这里以Mac电脑为例
首先就是pom.xml文件的配置,如下图
具体内容如下:
<?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>org.example</groupId>
<artifactId>sd-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 所有需要引入的jar包的坐标-->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4.一个好的程序员是擅长在Junit下进行代码的运行,应为这里你可对任何一个部分进行单独的运行,从而检查每一部分是否出错,因此下面就是对Junit的配置
首先得导入Junit4 jar包和hamcrest-core-1.3.jar的导入,方法如下
(1)首先点击文件->项目结构,如下图
(2)在下面界面中方框中则为我已经导入的jar包,如果是第一次进入这个界面,可以点击箭头所指的加号将你下载的两个jar包给导入进去,并勾选对号
5.准备工作都做完之后下面就是真正的代码实现了
(1)首先是各种包的创建
(2)mybatis.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"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xiaomishop?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="连接你数据库的密码"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
(3)UserMapper.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.sd.dao.UserDao">
//数据的增加
<insert id="addUser" parameterType="com.sd.entity.User">
insert into xiaomishop.user(uid,telephone,password,email,nickname) values (#{uid},#{telephone},#{password},#{email},#{nickname});
</insert>
//数据库数据的修改
<update id="updateUser" parameterType="com.sd.entity.User">
update xiaomishop.user set telephone=#{telephone},password=#{password},email=#{email},nickname=#{nickname} where uid=#{uid};
</update>
//查询数据库中所有的数据
<select id="findAllUser" resultType="com.sd.entity.User">
select * from user
</select>
//按照某个字段来进行查询
<select id="getUserById" parameterType="int" resultType="com.sd.entity.User">
select *from xiaomishop.user where uid=#{uid}
</select>
//删除
<delete id="deleteUser" parameterType="com.sd.entity.User">
delete from xiaomishop.user where uid=#{uid};
</delete>
</mapper>
(4)UserDao编写
package com.sd.dao;
import com.sd.entity.User;
import java.util.List;
/**
* @description:用户dao接口
* @modified By:
* @version: 1.0
*/
public interface UserDao {
//获取全部用户
List<User> findAllUser();
//根据ID查询
User getUserById(int uid);
//insert一个用户
int addUser(User user);
//修改用户
int updateUser(User user);
//删除数据
int deleteUser(int uid);
}
(5)User实体类
package com.sd.entity;
/**
* @date :Created in 2021/4/20 11:50
* @description:用户实体类
* @modified By:
* @version: 1.0
*/
public class User {
private int uid;
String telephone;
String password;
String email;
String nickname;
public User(){
}
public User(int uid,String telephone,String password,String email,String nickname){
this.uid=uid;
this.telephone=telephone;
this.password=password;
this.email=email;
this.nickname=nickname;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", telephone='" + telephone + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
(6)MysqlsessionFactory编写
package com.sd.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
* @date :Created in 2021/4/20 11:52
* @description:sqlsession工厂类
* @modified By:
* @version: 1.0
*/
public class MySqlsessionFactory {
static String resource="mybatis.xml";
static SqlSessionFactory sqlSessionFactory;
static {
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* @description: 获取sqlsession
* @create time: 2020/12/9 10:29
* @return SqlSession
*/
public static SqlSession getSQLSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
(7)Mytest编写
package com.sd.test;
import com.sd.dao.UserDao;
import com.sd.entity.User;
import com.sd.util.MySqlsessionFactory;
import org.apache.ibatis.session.SqlSession;
import javax.jws.soap.SOAPBinding;
import java.util.List;
/**
* @date :Created in 2021/4/20 11:54
* @description:测试mybatis
* @modified By:
* @version:
*/
public class MyTest {
public static void main(String[] args) {
SqlSession sqlSession = MySqlsessionFactory.getSQLSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> allUser = mapper.findAllUser();
for (User user : allUser) {
System.out.println(user.toString());
}
System.out.println("===================");
getUserById();
System.out.println("===================");
//addUser();
System.out.println("===================");
updateUser();
System.out.println("============");
deleteUser();
}
public static void getUserById(){
SqlSession sqlSession = MySqlsessionFactory.getSQLSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
User userById = mapper.getUserById(23);
System.out.println(userById);
sqlSession.close();
}
public static void addUser(){
SqlSession sqlSession = MySqlsessionFactory.getSQLSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
int user = mapper.addUser(new User(46, "186", "1863857", "[email protected]", "唐务强"));
if(user>0){
System.out.println("插入成功");
}
sqlSession.commit();
sqlSession.close();
}
public static void updateUser(){
SqlSession sqlSession = MySqlsessionFactory.getSQLSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.updateUser(new User(23,"TWQ","twq","143095","唐务强666"));
sqlSession.commit();
sqlSession.close();
}
public static void deleteUser(){
SqlSession sqlSession = MySqlsessionFactory.getSQLSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.deleteUser(1);
sqlSession.commit();
sqlSession.close();
}
}
(8)在UserDao里Mac版按command+N,Window按alt+shift+insert,然后在出来的界面中点击测试尽力啊之后如下第二张图
然后就进入
当然这里要把你进行的增删改查的内容给复制过来,这张图片就是在Junit环境下的方便之处,可以看到每一个方法左侧都有一个绿色的可运行的按钮,在这里你可单独运行某个模块,而不影响其他的模块