其他分享
首页 > 其他分享> > mybatis基本使用

mybatis基本使用

作者:互联网

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>
<properties resource="db.properties"/>
<typeAliases>
<!--<typeAlias type="com.xxx.pojo.User" alias="User"></typeAlias>-->
<package name="com.xxx.pojo"></package>
</typeAliases>
<environments default="test">
<!--<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/mybatis?useSSL=false&amp;"/>-->
<!--<property name="username" value="root"/>-->
<!--<property name="password" value="123456"/>-->
<!--</dataSource>-->
<!--</environment>-->
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/xxx/dao/UserMapper.xml"/>
</mappers>
</configuration>

MybatisUtils.java
package com.xxx.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;

/**
* @author xzy
* @create 2022/5/26 21:16
*/
public class MybatisUtils {

static private SqlSessionFactory sqlSessionFactory;
public MybatisUtils(){
}

static public SqlSession getSqlSession(){
try {
String resource = "mybatis-config.xml";
//获取SqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory.openSession();
}
}
 
User.java
package com.xxx.pojo;

import org.apache.ibatis.type.Alias;

/**
* @author xzy
* @create 2022/5/26 21:31
*/
@Alias("User")
public class User {
private int iduser;
private String username;
private String pwd;

public User() {
}

public User(int iduser, String namename, String pwd) {
this.iduser = iduser;
this.username = namename;
this.pwd = pwd;
}

@Override
public String toString() {
return "User{" +
"iduser=" + iduser +
", namename='" + username + '\'' +
", pwd='" + pwd + '\'' +
'}';
}

public int getIduser() {
return iduser;
}

public void setIduser(int iduser) {
this.iduser = iduser;
}

public String getNamename() {
return username;
}

public void setNamename(String namename) {
this.username = namename;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}
}
 
 
UserMapper.java
package com.xxx.dao;

import com.xxx.pojo.User;

import java.util.List;
import java.util.Map;

/**
* @author xzy
* @create 2022/5/26 21:37
*/
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}

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.xxx.dao.UserMapper">

<select id="getUserList" resultType="user">
SELECT * FROM mybatis.user;
</select>
<select id="getUserById" parameterType="int" resultType="com.xxx.pojo.User">
SELECT * FROM mybatis.user where iduser = #{id}
</select>
<insert id="addUser" parameterType="com.xxx.pojo.User">
insert into mybatis.user (iduser,username,pwd) value (#{iduser},#{username},#{pwd});
</insert>
<update id="updateUser" parameterType="com.xxx.pojo.User">
update mybatis.user set username=#{username},pwd=#{pwd} where iduser=#{iduser};
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where iduser = #{id};
</delete>

</mapper>
 

标签:基本,String,--,iduser,pwd,使用,mybatis,public
来源: https://www.cnblogs.com/bitterteaer/p/16533371.html