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操作数据库
基础教程推荐
猜你喜欢
- kafka添加安全验证配置方式 2023-06-24
- Springcloud Stream消息驱动工具使用介绍 2023-05-18
- jsp传参 servlet接收中文乱码问题的解决方法 2023-08-02
- Java实现一致性Hash算法详情 2023-05-14
- springboot读取resources下文件的方式详解 2023-01-23
- Java实现学生管理系统详解流程 2023-01-29
- Java中BigDecimal,DateFormatter 和迭代器的"陷阱" 2022-12-16
- Clojure 与Java对比少数据结构多函数胜过多个单独类的优点 2022-12-16
- java词法分析器DDL递归应用详解 2023-03-06
- SpringBoot设置动态定时任务的方法详解 2022-12-27