1.db.propertiesデータベースの設定
jdbc.driverClassName =はcom.mysql.jdbc.Driver
jdbc.url = JDBCます。mysql://127.0.0.1:3306 / MyBatisの
jdbc.username =ルート
jdbc.password =ルート
2.applicationContext.xmlプロフィール
<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> < 豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" のxmlns:P = "http://www.springframework.org/schema/p" のxmlns:コンテキスト= "http://www.springframework.org/schema/context" のxmlns:AOP =" HTTP: //www.springframework.org/schema/aop」のxmlns:JEE = "http://www.springframework.org/schema/jee" のxmlns:TX = "http://www.springframework.org/schema/tx" XSI:schemaLocationの =」 http://www.springframework.org/schema/beansます。http: http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee HTTP:// WWW .springframework.org /スキーマ/ JEE /春jee.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd HTTP:// http://www.springframework.org/schema/tx/spring-tx.xsd www.springframework.org/schema/tx " > <! - 1.インポートデータベース構成ファイル- > < コンテキスト:不動産-プレースホルダLOCATION = "CLASSPATH:db.properties" /> 2.オープン@Autowiredコメント- > < コンテキスト:アノテーション設定/> <! - <!- 3.春豆の走査アセンブリ- > < コンテキスト:スキャンコンポーネントベース・パッケージ= "com.gzcgxt.erp" /> < ! - 4.jdbcTemplateデータベース操作部品- > < ビーンID = "jdbcTemplate" クラス= "org.springframework.jdbc.core.JdbcTemplate" > < コンストラクタ-ArgのREF = "データソース" /> </ ビーン> <! - 5.Druidデータソースの構成- > < ビーンID = "データソース」 クラス= "com.alibaba.druid.pool.DruidDataSource" > < プロパティ名の値= "$ {jdbc.driverClassName}" /> < プロパティ名= "URL" 値= "$ {jdbc.url}" /> < プロパティ名= "ユーザ名" 値= "$ {jdbc.username}" /> < プロパティ名= "パスワード" 値= "$ {jdbc.password}" /> </ 豆> </ 豆> =「driverClassName」
3.Userオブジェクト
パッケージcom.gzcgxt.erp.domain。 輸入lombok.Data。 @data パブリック クラスユーザー{ プライベート整数ID。 プライベート文字列名; プライベート整数歳。 プライベート整数フラグ。 }
4.UserDAO
パッケージcom.gzcgxt.erp.dao。 インポートのjava.sql.ResultSet; 輸入ます。java.sql.SQLException; 輸入はjava.util.List; インポートするjavax.sql.DataSource; 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.jdbc.core.JdbcTemplate。 輸入org.springframework.jdbc.core.RowMapper; 輸入org.springframework.stereotype.Repository; 輸入com.gzcgxt.erp.domain.User。 @Repository パブリック クラスUserDAO { @Autowired プライベートJdbcTemplate jdbcTemplate。 公共 のボイド保存(){ jdbcTemplate.updateは( "user`・セット名= 'DDDD'、年齢= 12、フラグ= 1 'INSERT INTO" )。 } 公共 ボイドは(){削除 リスト <ユーザ>リスト= jdbcTemplate.queryは( "IDのASC制限10によって、ユーザ注文SELECT * FROM" 、 新しい RowMapperの<ユーザー> (){ @Override パブリックユーザmapRow(のResultSet RS、INT たrowNum)を スローのSQLException { ユーザU = 新しいユーザー(); u.setId(rs.getInt( "ID" ))。 u.setName(rs.getString( "名前" )); u.setAge(rs.getInt( "年齢" )); u.setFlag(rs.getInt( "フラグ" ))。 返すuと。 } })。 ユーザU =はlist.size()> 1?list.get(0):NULL ; ストリングcmdTextは = "user`` FROM DELETE WHERE ID =" + u.getId()。 jdbcTemplate.execute(cmdText)。 System.out.println(U + "删除成功" ); } }
6.テストコード
パッケージcom.gzcgxt.erp。 輸入org.junit.Test; 輸入org.junit.runner.RunWith; 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.test.context.ContextConfiguration。 輸入org.springframework.test.context.junit4.SpringJUnit4ClassRunner。 輸入com.gzcgxt.erp.dao.UserDAO。 @RunWith(SpringJUnit4ClassRunner。クラス) @ContextConfiguration( "クラスパス:applicationContext.xmlを" ) パブリック クラスのApp { @Autowired プライベートUserDAOのDAO。 @Test 公共 のボイドでTestApp()はスロー例外{ dao.deleteを(); } }