Java操作数据库

1. JDBCspring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgresspring.datasource.username=postgresspring.datasource.password=deb.debspring.datasource.driver-class-name=org.postgresql.Driver...

1. JDBC

spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=deb.deb
spring.datasource.driver-class-name=org.postgresql.Driver


@Service
public class UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public List<User> getList(){
        String sql = "SELECT id, name, password FROM test.user ";
        return (List<User>) jdbcTemplate.query(sql, new RowMapper<User>(){
          @Override
          public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setName(rs.getString("name"));
            user.setPassword(rs.getString("password"));
            return user;
          }
     
        });
      }

}

2. Mybatis

  • 使用xml
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.main.model


public interface UserMapper {
    List<UserEntity> getAll();
    
    UserEntity getOne(Long id);

    void insert(UserEntity user);

    void update(UserEntity user);

    void delete(Long id);
}


<?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>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
</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="com.main.mapper.UserMapper" >
    <resultMap id="BaseResultMap" type="com.main.model.UserEntity" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="passWord" property="passWord" jdbcType="VARCHAR" />
        <result column="user_sex" property="userSex" jdbcType="VARCHAR"/>
        <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
    </resultMap>
    
    <sql id="Base_Column_List" >
        id, userName, passWord, user_sex, nick_name
    </sql>

    <select id="getAll" resultMap="BaseResultMap"  >
       SELECT 
       <include refid="Base_Column_List" />
       FROM test.userentity
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT 
       <include refid="Base_Column_List" />
       FROM test.userentity
       WHERE id = #{id}
    </select>

    <insert id="insert" parameterType="com.main.model.UserEntity" >
       INSERT INTO 
            test.userentity
            (userName,passWord,user_sex) 
        VALUES
            (#{userName}, #{passWord}, #{userSex})
    </insert>
    
    <update id="update" parameterType="com.main.model.UserEntity" >
       UPDATE 
            test.userentity
       SET 
        <if test="userName != null">userName = #{userName},</if>
        <if test="passWord != null">passWord = #{passWord},</if>
        nick_name = #{nickName}
       WHERE 
            id = #{id}
    </update>
    
    <delete id="delete" parameterType="java.lang.Long" >
       DELETE FROM
             test.userentity 
       WHERE 
             id =#{id}
    </delete>

</mapper>
  • 使用mapper

3. JPA(Hibernate)

本文标题为:Java操作数据库

基础教程推荐