创建项目
使用IDEA创建Maven工程
IDEA配置Maven
设置好Maven版本、配置文件以及Maven仓库
连接数据库
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bysj?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
SQL语句
select语句
<select id="load" parameterType="java.lang.Integer" resultMap="noteResult">
select * from note where id=#{id}
</select>
select多条语句
<select id="query" parameterType="note" resultMap="noteResult">
select * from note
<where>
<if test="id != null and id != ''">
and id like concat('%',#{id},'%')
</if>
<if test="title != null and title != ''">
and title like concat('%',#{title},'%')
</if>
<if test="author != null and author != ''">
and author like concat('%',#{author},'%')
</if>
<if test="content != null and content != ''">
and content like concat('%',#{content},'%')
</if>
</where>
</select>
insert语句
<insert id="add" parameterType="note">
insert into note(title,author,content) values(#{title},#{author},#{content})
</insert>
update语句
<update id="update" parameterType="person">
update note set title=#{title},author=#{author},content=#{content} where id= #{id}
</update>
delete语句
<delete id="delete" parameterType="java.lang.Integer">
delete from note where id=#{id}
</delete>
执行结果