MyBatis⼊门-实现数据库的增删改查(代码篇)MyBatis⼊门级代码篇:
1. 建⽴⼀个Maven Java⼯程
2. 在l中导⼊依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyBatis03</groupId>
<artifactId>MyBatis03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>MyBatis03 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>ample</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenpiler.source>1.7</mavenpiler.source>
<mavenpiler.target>1.7</mavenpiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<finalName>MyBatis03</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see /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.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
新闻模板html
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</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、编写⼀个⽤户表t_user
use mybatis;
drop table if exists t_user;
mysql菜鸟教程增删改查
create table t_user(
user_id int(11)not null auto_incremet,
username varchar(20)default null,
password varchar(20)default null,
create_time varchar(20)default null,
primary key(user_id))ENGINE=MyISAM DEFAULT CHARSET=utf8;
4、编写⼀个⽤户的实体类
import java.util.Date;
public class User {
private Integer userId;
private String username;
private String password;
private Date createTime;
public Integer getUserId(){
return userId;
}
@Override
public String toString(){
return"User{"+
"userId="+ userId +
", username='"+ username +'\''+
", password='"+ password +'\''+
", createTime="+ createTime +
'}';
}
public void setUserId(Integer userId){
this.userId = userId;
}
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 Date getCreateTime(){
return createTime;
}
public void setCreateTime(Date createTime){
}
}
5、编写⼀个UserMapper.java接⼝类
import ity.User;
import java.util.List;
public interface UserMapper {
public void insert(User user);//1、插⼊⽤户
vb技术是指什么public User selectById(Integer id);//2、查询单个⽤户
public List<User>selectAll();//3、查询所有⽤户信息
public void updateById(User user);//4、更新⽤户信息
public void deleteById(Integer id);//5、删除
}
6、编写⼀个l配置⽂件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="batis.mapper.UserMapper">
<!--mybatis插⼊实例⽤户信息-->
<insert id="insert"parameterType="ity.User">
insert into t_user(username,password,create_time) values(
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{createTime,jdbcType=DATE,javaType=date}
);
</insert>
<!--查询单个⽤户信息-->
<select id="selectById"parameterType="int"resultType="ity.User">        select user_id, username,password,create_time from t_user
where user_id=#{userId}
</select>
<!--查询所有⽤户信息-->
<select id="selectAll"resultType="ity.User">
select user_id,username,password,create_time from t_user
</select>
<!--更新⽤户信息-->
<update id="updateById"parameterType="ity.User">
update t_user set username=#{username},password=#{password} where user_id=#{userId}
</update>
<!--删除⽤户信息-->
java语言代码大全新手入门<delete id="deleteById"parameterType="int">
delete from t_user where user_id=#{userId}
</delete>
</mapper>
7、编写⼀个l配置⽂件
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration PUBLIC
"-///DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl"value="LOG4J"/>
</settings>
库管理系统
<environments default="testMysql">
<environment id="testMysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver"value="sql.jdbc.Driver"/>
<property name="url"value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/> <property name="username"value="root"/>
<property name="password"value="165217"/>
</dataSource>
</environment>
</environments>
<!--加载mybatis映射⽂件mapper-->
<mappers>
<mapper resource="www/javaapi/com/cn/mybatis/l"/>
</mappers>
</configuration>
8、写⼀个UserTest.java的测试类
package st;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;实例句柄是什么
import ity.User;
import batis.mapper.UserMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class UserTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init(){
InputStream inputStream=null;
try{
//加载mybatis配置⽂件
inputStream=org.apache.ibatis.ResourceAsStream("l");
}catch(IOException e){
e.printStackTrace();
}
//使⽤mybatis API操作
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
sqlSessionFactory=builder.build(inputStream);
}
//2、mybatis插⼊数据
@Test
public void insert(){
User user=new User();
user.setUsername("张三");
user.setPassword("123456");
user.setCreateTime(new Date());
SqlSession session=sqlSessionFactory.openSession();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。